-
sizeof array
Hi,
Was trying this program
Code:
#include<stdio.h>
int retsize( int a[] );
int main()
{
int arr[]={1,2,3,4};
printf(" size = %d \n",sizeof(arr));
printf(" size = %d \n",retsize(arr));
return 0;
}
int retsize (int arr[])
{
return sizeof(arr);
}
The answer im getting is
size = 16
size = 4
i wonder why the function is not returning 16 too.
please shed some light. and how do i correct it
Thanks
MAx
-
It is not possible to pass an array to a function. Instead, a pointer to the first element is passed (and as you see, pointers are four bytes).
-
And if you actually want to use the size of an array when it has been passed to a function, you need to pass the original size from the calling code - that is, add another parameter to the function that indicates the size.
--
Mats