c - Find the length of a string array in the form of char**? -
this question has answer here:
i know how 1 find length of string array initialised way
char* arr[] = {"some", "thing"};
just this,
size_t length = sizeof(arr)/sizeof(char*);
and length 2, yes? had assumed char** same thing char* array above, , whole sizeof thing not work (i @ least know pointer pointer). have looked on place trying find way length of array stored char** opposed char* [], feel don't understand enough on how this, though seems should straightforward.
in program, have char** array values @ indexes 0...n, when try getting length using way above, length 1.
any help?
there's way it:
char* arr[] = {null, null, null}; printf("%d\n", sizeof(arr)/sizeof(*arr));
this prints out 3
. there's problem approach, though, might not aware of - works if arr
size known compile time - means, exact size provided in square braces (and should use constants instead of sizeof
anyway), or initialization list provided in both of our examples (and think it's legitimate case of sizeof
arrays). if define array in other way, arr2
below:
char** arr2 = arr;
it won't work, , 1
, say.
Comments
Post a Comment