Possible Duplicate:
C sizeof a passed array
instead of sending the sizeof:
void main()
int x[] = {1, 2, 3, 1, 3, 0, 6};
dupes(x, sizeof x / sizeof x[0]);
...
}
int dupes(int a[], int n)
...
...
why cant i call it with only the array:
int main()
{
int x[] = {1, 2, 3, 1, 3, 0, 6};
dupes(x);
...
}
int dupes(int a[])
{
int n = sizeof a / sizeof a[0];
when i do this the size always gets 1 – and it cant be 1 casue i’m sending an array with 7 elements!
It’s because when You pass an array to a function, it is copied into a pointer and the information about it’s size is lost. In the called function it is just a normal pointer.
So size of (a) becomes 4 (because its a pointer) and sizeof (a[0]) is also 4 (because its an integer), so n equals 1