Mail Archives: djgpp/1999/03/05/20:56:26
Message-ID: | <36E08691.ED519155@jps.net>
|
Date: | Fri, 05 Mar 1999 17:36:17 -0800
|
From: | Dennis Yelle <dennis51 AT jps DOT net>
|
X-Mailer: | Mozilla 4.5 [en] (Win95; U)
|
X-Accept-Language: | en
|
MIME-Version: | 1.0
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Array question (a little bit newbie)
|
References: | <7bllsm$vap$1 AT news5 DOT svr DOT pol DOT co DOT uk>
|
NNTP-Posting-Host: | 209.239.203.10
|
X-NNTP-Posting-Host: | 209.239.203.10
|
X-Trace: | 6 Mar 1999 01:57:58 -0800, 209.239.203.10
|
Lines: | 71
|
X-NNTP-Posting-Host: | 209.63.224.240
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Andrew Davidson wrote:
>
> I'm having a spot of bother defining an 'unsized' array of char pointers.
> What I want is an array filled with pointers to pointers to a list of chars,
> a little like:
>
> char *mylist[]= {
> "item 1", "item 2", "item 3"
> };
>
> (incidentally, why can't I get a compile on char **mylist= {"item1", "item
> 2","item3"}; if I can do char **argv in main()? )
The reason you can say
int main( int argc, char **argv)
and then
printf( "%s\n", argv[0]);
is because SOMEBODY ELSE wrote the code to
allocate an array of (char *) and passed a pointer to it, to your
main().
>
> The difference is I don't want a list of pointers to strings, I want
> pointers to lists of chars of varying length.
Well, you don't say if you are using C or C++.
C doesn't know anything about lists of chars of varying length,
but, depending on what you mean by that, you CAN do it yourself,
with some difficulty. But you need to be more specific.
Do the lengths need to change at run time, or do they just need
to be different? Can you say in advance that none of them will be
more than, say 100 bytes long?
C++, on the other hand comes with strings which are a much
more convenient way of doing things like this.
> Oh and while I'm at it is there any way of calculating the length of any of
> these lists without using terminating characters or placing a byte count at
> the front of each list? Would sizeof(mylist[<list number>]) work?
Well, again, it depends on what you want.
I often use
#define lengthof(x) (sizeof(x)/sizeof((x)[0]) )
so that I can say
char *things[77];
and then later say
for( i=0; i<lengthof(things); i++)
or did you want the total length of the strings?
total_len = 0;
for( i=0; i<lengthof(things); i++)
total_len += strlen( things[i]);
but this does not include the trailing '\0' byte.
Now, if you want the 77 above to be variable,
you need to do something like
char **stuff;
stuff = calloc( sizeof( *stuff), number);
and then fill it up like this
stuff[0] = strdup( "this is a test");
stuff[1] = strdup( "test 2");
>
> Thanks for you time, sorry to be so unclued about such a basic point. I've
> tried looking it up in a couple of C manuals but they don't seem to be
> interested in anything past string arrays... Maybe I ought to buy a new
> book.
>
> Andrew
- Raw text -