Possible Duplicate:
length of array in function argument
I am trying to get the length of an integer array but i am not getting the right answer
void main()
{
int x[]={33,55,77};
printf("%d",getLength(x));//outputs 1
printf("%d",sizeof(x)/sizeof(int));//outputs 3
}
int getLength(int *inp)
{
return sizeof(inp)/sizeof(int);
}
So why is getLength returning value 1 instead of 3 ?
Since arrays decay to pointers, you cannot perform length calculation in a function: the function gets a pointer, not an array. You need to do the computations inline the way your function does when it prints
3, or use a macro to compute array length:Here is a link to a demo on ideone.