This might be a stupid question, but I thought arrays start from index 0. So when we say print_array(data, 3)why is it only printing 10 20 30? why 40 is not included? isnt 40 index 3?!
I will appreciate if anyone can clarify this for me.
Code:
#include <stdio.h>
void print_array(int *p, int size) { /* int *p could have been written as int p[] */ int idx = 0;
for (idx = 0; idx < size; idx++) { printf("%d\n", p[idx]); }
printf("In print_array function size of array using sizeof: %ld\n", sizeof(p));}
int main() { int data[] = {10, 20, 30, 40, 50, 60};
print_array(data, 3); /* data = NULL; What happens if we try this assignment? */
printf("Size of data array using sizeof: %ld\n", sizeof(data));
return 0;}
Code:
out put is:
10
20
30
In print_array function size of array using sizeof: 4
Size of data array using sizeof: 24