Possible Duplicate:
Sizeof an array in the C programming language?
Why does a C-Array have a wrong sizeof() value when it’s passed to a function?
See the below code and suggest me that what is the difference of "sizeof" keyword when I used like this:
#include<stdio.h>
#include<conio.h>
void show(int ar[]);
void main()
{
int arr[]={1,2,3,4,5};
clrscr();
printf("Length: %d\n",sizeof(arr));
printf("Length: %d\n",sizeof(arr)/sizeof(int));
show(arr);
getch();
}
void show(int ar[])
{
printf("Length: %d", sizeof(ar));
printf("Length: %d", sizeof(ar)/sizeof(int));
}
But the output is like this:
Output is:
Length: 10
Length: 5
Length: 2
Length: 1
why I am getting like this; If I want to take the entire data from one array to another array the how can I do?
Suggest me If anyone knows.
Arrays decay to pointers in function calls. It’s not possible to compute the size of an array which is only represented as a pointer in any way, including using
sizeof.You must add an explicit argument:
In the call, you can use
sizeofto compute the number of elements, for actual arrays:Note that
sizeofgives you the size in units ofchar, which is why the division by what is essentiallysizeof (int)is needed, or you’d get a way too high value.Also note, as a point of interest and cleanliness, that
sizeofis not a function. The parentheses are only needed when the argument is a type name, since the argument then is a cast-like expression (e.g.sizeof (int)). You can often get away without naming actual types, by doingsizeofon data instead.