Well, actually NULL termination is almost always the right idea with a array of pointers and is THE WAY to pass matrices around in 'C'. I will grant you some exceptions, but they are few, very few. I find your claim rather odd.
Update:
First claim: wastes space: Nonsense! Memory is usually allocated in increments of (8 or 16 pointers)x X, where X >=1. On my machine (Win XP), a pointer takes the same size as an int. Memory is allocated in hunks of 128 bytes or 32 "words" of 4 bytes each. So the storage of the NULL pointer will require one more allocation unit 1 of 32 times with random number of things in a list. Storage of a "count" will require at least that much and probably more because now we have an "extra thing" in addition to the pointers to data. Now we get into a an "object" in OO-ese and depending upon how this is implemented, this could wind up taking a lot more storage to represent a 2D array!
Second Claim: if you need random access, this means you have to
calculate the length anyway, by scanning the whole thing looking for the
NULL, before you can access it at all. Well of course not! If "you" made this structure, with the intention of using/modifying it, you will know how "big it is". I will show some code below that accesses a char** array. 'C' doesn't have any limits on how array indices are calculated. C assumes that you "know what you are doing", and you can screw-up massively!
Update: this code below does screw-up. It works because the data array was declared and assigned all at once. Like I said above: you can screw-up massively!". I did it also! In general each "row" will not have a correlation with another "row". See how this can fool me?(and you). And is exactly to my point of using pointers to traverse a char** structure instead of indicies. This doesn't mean that "random access" isn't possible..it is! Just like below. BUT you have to know how many things are on THAT ROW, ie the number of COLUMNS for that row.
Many matrices have the same number of columns for each row, a char ** usually does not. The determination of number of rows is a trivial thing...just like in Perl! If the rows are "ragged", non-equal number of columns, then things get more complex...just like in Perl!
include <stdio.h>
int main()
{
char *data[] = {"some stuff",
"ABCEDFGHIJ",
"and third stuff",
NULL
};
printf ("%c\n",data[0][28]); /*prints "i", in third*/
printf ("%c\n",data[2][7] ); /*prints "r", in third*/
printf ("%c\n",data[2][6] ); /*prints "i", in third*/
printf ("%c\n",data[1][23]); /*prints "u", in stuff*/
printf ("%c\n",data[2][-9]); /*prints "C", in ABC...*/
printf ("%c\n",data[1][-5]); /*prints "t", in stuff*/
return(0);
}
| [reply] [d/l] |