I'm trying to use the trick to get the number of items in an array by dividing the array size by the size of the type: sizeof(array) / sizeof(<type>)
or sizeof(array) / sizeof(array[0])
For reasons I don't fully understand, when I try to take an array as an argument to a function as type const char**
and then call sizeof()
, it always returns 8.
#include <stdio.h>
void write(const char** arr) {
printf("%ld", sizeof(arr));
}
int main() {
const char* word[] = {
"asdf",
"qwer",
"hjkl"
};
write(word);
return 0;
}
>> 8
So sizeof(array) / sizeof(const char*)
always just comes out to 1.
What's going on?