You can determine the size:
// file.c char *myArray[100] = { "str1", "str2", ... "str100" }; // file.h extern char *myArray[100];
Then sizeof should work, but you can also just #define specify the size or set the variable.
Other options are to count the length and save it once in your code ...
// file.c char *myArray[] = { "str1", "str2", ... "strN", (char *) 0 }; int myArraySize = -1; int getMyArraySize() { if( myArraySize < 0 ) { for( myArraySize = 0; myArray[myArraySize]; ++myArraySize ); } return myArraySize; } // file.h extern char *myArray[]; int getMyArraySize();
This would give an unknown size at compile time. If the size is known at compile time, just saving a constant will save the overhead of counting.
source share