I'm trying to calculate the length of an array using the sizeof() function in C language. The following is my code.
Code
#include <stdio.h>
void printArraySize(int array[]);
int main() {
int array[] = {1, 2, 3, 4, 5, 6};
int size = sizeof(array) / sizeof(array[0]);
printf("Size of array - %d\n", size);
printArraySize(array);
return 0;
}
void printArraySize(int array[]) {
int size = sizeof(array) / sizeof(array[0]);
printf("Size of array - %d\n", size);
}
When I'm calculating the length of the array inside the main function, the output is correct. But when I try to do the same inside the function printArraySize() which accepts an integer array, the output is always 1.
I'm new to C language and I'm getting so confused with this. Can anyone please explain to me the reason behind the same and also suggest the code which can calculate the length of an array inside a function.
Thank you