In C:
How do you find the number of elements in an array of structs, after sending it to a function?
int main(void) {
myStruct array[] = { struct1, struct2, struct3, struct4, struct5, struct6 };
printf("%d\n", sizeof(array));
printf("%d\n", sizeof(array[0]));
f(array);
}
void f(myStruct* array) {
printf("%d\n", sizeof(array));
printf("%d\n", sizeof(array[0]));
}
For some reason the printf in main shows different results than the printf in f.
My need is to know how many elements are in the array.
You can’t.
You have to pass the size to the function, eg:
Also notice that in
farray is a pointer, while inmainit is an array. Arrays and pointers are different things.