More 2D array woes...
Amateurgrammer
eychaner at suncub.bbso.caltech.edu
Mon Mar 4 09:51:14 AEST 1991
Here we go again, warm up the flame guns...
Ok, suppose I have an array like
char strings[NUMBER][SIZE];
How do I add more strings to this array, i.e. make it larger, portably and
easily. I can't realloc it, since it's not a pointer! Argh! Is there some
sneaky way to declare it so that I can do what I want, or should I just use
char *strings;
and a lot of pointer arithmetic? How about
char **strings;
strings = (char **) calloc (NUMBER, sizeof (char *));
strings[0] = (char *) malloc (SIZE);
strings[1] = (char *) malloc (SIZE);
...
(I don't care if the 2d array is contiguous or not...)
Then strings is a pointer and I can realloc it, allowing me to add more
strings! And can I even access them as
(strings [string_num]) [string_pos] = 'x';
which becomes
*(*(strings + string_num) + string_pos) = 'x';
or does it? I'm not sure....
:-)
Glenn Eychaner - Big Bear Solar Observatory - eychaner at suncub.bbso.caltech.edu
"You Have the Right to Remain DEAD."
-The Simpsons
More information about the Comp.lang.c
mailing list