delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/03/10:23:11

Message-Id: <199909030938.FAA17194@delorie.com>
From: "Dan Gold" <TedMat AT CoastNet DOT com>
To: <djgpp AT delorie DOT com>
Subject: Re: How to make allocate an array of strings?
Date: Fri, 3 Sep 1999 09:52:38 -0700
X-MSMail-Priority: Normal
X-Priority: 3
X-Mailer: Microsoft Internet Mail 4.70.1155
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Continued...

On the topic of...

    char (*p)[SIZE] = malloc(number*SIZE); /* C */

I didn't really understand the declaration? Like if I went char *array[20];
 I would get an array of character pointers and if I wanted an array of
pointer to pointers I would use **array[20]. .  I'm confused about the
parenthesis and what it changes to say the least?  I don't see how it
understands you want an array of pointers? Is SIZE the number of pointers
or the size of data each one points to, it looks like both since the array
is [SIZE] and each index points to *SIZE?, I realize the brackets has
something to do with that.  Well I guess I just need it broken down into
bitesize chunks. 

If I were to define an array of pointers I would go:
     char *p[20];  // 20 pointers  

If I were to allocate an array of pointers I would go:
    char **p = malloc(20 * sizeof(char *)); 

Is this right?

is the double pointer necessary or can you have a single pointer to an
array of pointers when allocating? or must a pointer to a pointer always be
a **pointer?

    char *p = malloc(20 * sizeof(char *)); // impossable?

If I were to create an array of pointers, I would then allocate the data
for them like this?

     for (i=0; i<number; i++) {
          p[i] = malloc(SIZE_OF_DATA);
     }
   
Just to make sure I understand...

Is char ** p = malloc [number * sizeof *p]; and
   char ** p = malloc [number * sizeof(char *); are equal...

whye does it matter since they are both handled by the preprocessor? I have
heard the preffered way is to use *p but I dont see why.

Sorry for the Overkill. Thanks for clarifying this.

From ((--Dan|Gold--))

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019