LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 06: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, 08: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, 09: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, 05: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



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 11:43 AM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 10:47 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 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
Open Source Consulting | Domain Registration