I have this code:
int indexOf(const char *array[], char *e)
{
printf("inside: %d\n",(int)sizeof(array));
/* ... */
}
int main(int argc, char *argv[])
{
const char *a[] = {";", ",", ":", "==", ":="};
char *b = "==";
printf("outside: %d\n",(int)sizeof(a));
int d = indexOf(a,b);
/* ... */
}
And this is the output:
outside: 40
inside: 8
Why output is not the same? Any help, please?
The array decays into a pointer to it’s first element when passed to a function. The
sizeoffrom the function yields the size of the pointer on your implementation. You could have declared it:You will probably want to pass the length as a separate parameter.
EDIT
In that case you could mark the end of the array with a
NULLThat way in the function you will know where it ends.