LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-09-2003, 07:58 PM   #1
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
help w/ pointers vs arrays


I'm trying to understand how these things work right now and am running into a question that isn't answered in the book.

suppose that I have something like this:
Code:
.........
       char line[MAXLINE],
             *malloc(),
             *sptr,
             *strtbl[NLINES];

        while((nchars = get_line (line,MAXLINE)) != EOF &&
              (sptr = malloc ((unsigned) nchars +1)) != NULL )
        {
                strcpy (sptr, line);
                strtbl[i++] = sptr;
        }
.......
is it also legal to do this?:
Code:
        char *line,
                *sptr,
                **strtbl;
       
       while((nchars = get_line (line,MAXLINE)) != EOF &&
              (sptr = malloc (nchars +1)) != NULL )
        {
                strcpy (sptr, line);
                *(strtbl + i++) = sptr;
        }
I ask because it does work just the same both ways, but it seems like *(strtbl + i++) would end up pointing to someting else?

is this legal?
 
Old 04-09-2003, 09:04 PM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
char *strtbl[NLINES] ... reserves an array of NLINES char pointers in memory, strtbl is initialized to point to the first element of the array

char **strtbl ... is a pointer to a char pointer, but is not initialized to point anywhere (and no memory is reserved for storing the data array)


to make things more visible consider this:

typedef char *CPTR;
CPTR strtbl[NLINES]; /* reserves array and points to 1st element */

versus

typedef char *CPTR;
CPTR *strtbl; /* points nowhere */


hope it helps :-)
 
Old 04-09-2003, 10:57 PM   #3
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
that does help. thanks.

is there a way around having the constant? maybe something like:

typedef char *CPTR;
CPTR *strtbl = malloc( nArraySize * sizeof(*int));

would this allocate space for nArraySize number of int* 's ?
 
Old 04-10-2003, 06:13 PM   #4
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
>typedef char *CPTR;
>CPTR *strtbl = malloc( nArraySize * sizeof(*int));

*int = not correct
int * = what you need

> would this allocate space for nArraySize number of int* 's ?

would allocate space for nArraySize INT pointers, but "strtbl" would point to CHAR pointers, which is probably not what you inteded :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 12:43 PM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 09:24 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 11:00 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 11:47 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM


All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration