I came around this similar question. But the advantage I have is that i know that each string is 260 char long.
Any hope?
int noOfStrings = sizeof(stringArray)/sizeof(stringArray[0]);
This doesn't work.
As it says. You cannot (unless it's a variable local to the function). Because of the way arrays are passed they degrade to pointers and all size information is lost (outside of the context of where the array was created because the compiler cannot make assumptions about it anymore).
You must explicitly pass the size (or always pass the same size).
What you posted would work if stringArray
were declared globally or that statement was inside the function where you declared it.
It depends on how stringArray was declared.
That works when:
int main()
{
char stringArray[500];
int noOfStrings = sizeof(stringArray)/sizeof(stringArray[0]);
...
}
but not in:
int function(char *stringArray)
{
int noOfStrings = sizeof(stringArray)/sizeof(stringArray[0]);
...
}
In the first case, stringArray is a constant of type char[500], and the sizeof gives the number of bytes allocated for the array.
In the second case, stringArray is a pointer to char, and sizeof gives the size of the pointer.
They are two completely different things, just look similar.
Please post your code. Your specific solution which wants to compute a value at run time is not possible at first glance.
However, you might be able to code/define a macro as:
#define strsize sizeof(stringArray) / sizeof(char *)
if stringArray is an array of char pointers, but without seeing your source it is difficult to recommend a specific solution.
Sometimes a solution while not directly possible is still achievable but by other methods!!