I was reading a csc placement paper where I read a question related to c language array’s sizeof() operator. Answer was something else then i expected it to be.
int DIMension(int array[]) {
return sizeof(array )/ sizeof(int);
}
main() {
int arr[10];
printf(“Array dimension is %d”, DIMension(arr));
}
This program in c language prints 1 as the answer. Why is that happening?
Because
int array[]is just a pointer and it’s size is same as ofint.I think you expected that size of
arrwill somehow be passed to function, but it doesn’t work that way. Size ofarrcan be determined only in same scope where it was declared, becausesizeofactually works at compile time.