I want to get the length of an array, say int array[] = {1, 2, 3, 4}
. I used sizeof
to do that.
int length(int array[])
{
return sizeof(array) / sizeof(int);
}
int main()
{
int array[] = {1, 2, 3, 4};
printf("%d
", length(array)); // print 1
printf("%d
", sizeof(array) / sizeof(int)); // print 4
}
So, why the sizeof(array)
in function length
returns the pointer size of array
? But in function main
, it works.
And, how should I modify the length
function to get an array's length?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…