a simple question that bugs me.
Say I have an array defined in main like so int arr[5]
. Now, if I'm still inside main and I set int i = sizeof(arr)/sizeof(arr[0])
then I is set to be 5, but if I pass the array as a function parameter and do the exact same calculation in this function, I get a different number. Why is that? At first I thought its because in a function arr
is a pointer, but as far as I know arr
is a pointer inside main too!
Also, if I do something very similar only I initialize the array dynamically, I get weird results:
int *arr = (int*) malloc(sizeof(int) * 5);
int length = sizeof(*arr) / sizeof(arr[0]);
printf("%d
",length);
Here the output is 1
. Any ideas why?
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…