-1

Possible Duplicate:
Sizeof an array in the C programming language?
Why sizeof(param_array) is the size of pointer?

void printSizeOfArray(int a[])
{
    printf("%lu\n", sizeof(a));
}

int main()
{
    int it;
    int a[4] = {0};
    printSizeOfArray(a);

    return 0;
}

When I run the code I get 8. Why is a pointer to an array a pointer of type void?

And if a pointer to an array is of type void, why, when I write:

int *it;
int a[4] = {0};
it = a;
printf("%lu\n", sizeof(it));

It works ok as well? How can I point with an int?

Also, why does it print 8 in the second and not 4? It is a pointer to array (void*) and not an int.

Community
  • 1
  • 1
  • Why do you keep saying that a pointer to an array is of type void? The type of "array pointer" is dependant of what you have in your array. – ElPaco Nov 06 '12 at 12:20
  • `Why is a pointer to an array a pointer of type void?` First problem with this question is there are no pointers in your first block of code... Second overall question [pointers are not arrays, arrays are not pointers.](http://stackoverflow.com/questions/12676402/why-cant-i-treat-an-array-like-a-pointer-in-c) – Mike Nov 06 '12 at 12:26
  • Where has `Why is a pointer to an array a pointer of type void?` come from? – Maksim Skurydzin Nov 06 '12 at 12:37

3 Answers3

1

Arrays in C often "decay" to just pointers to the first element. This pointer doesn't retain any information about the size of the array, as you've noticed. When an array is passed to a function, such decay occurs.

The proper size can only be computed with the proper declaration in scope. So, you can make it work for functions, but then then size of the array becomes part of the function's prototype which is rarely useful:

void printSizeOfArray(int a[3]);

This is why typical C code always passes the size separately.

Basically, C's support for arrays is "weak".

unwind
  • 391,730
  • 64
  • 469
  • 606
1
int *a;    // this is a pointer
int arr[]; // this is an array. not a pointer, not a "pointer to void"

Why is a pointer to an array a pointer of type void?

  • It's not. It's an array. When you pass it to a function it decays into a pointer to an int. Why do you think it's a void *?

When I run the code I get 8.

  • That's the size of a pointer on your 64-bit system. Try this:

    int *a;
    printf("%d\n", sizeof(a));

  • You'll see it's 8, because a pointer on your system takes 8 bytes.

And if a pointer to an array is of type void, why, when I write: int *it = a; It works ok as well? How can I point with an int?

  • That's because it decays into a pointer to an int. You can use a void * to point to it as well because void * is typeless (it can point to anything).

Also, why does it print 8 in the second and not 4?

  • It should never print 4. You're question should be "why does it print 8 and not 32?". It's printing 8 because that's the number of bytes to represent a pointer to an int on your system and it is a pointer to an int. If you did sizeof(a) you should see a result of <array_size> * sizeof(int) so that's most likely 4*8 on your system or 32.
Mike
  • 47,263
  • 29
  • 113
  • 177
0

It's because of arrays decay into pointers in function calls, hence when you call a function with an array name, the argument in function is actually the pointer of that type so the sizeof() gives you the size of the pointer on that machine, that's why its printing 8

Mind one thing:

void printSizeOfArray(int a[])

and

void printSizeOfArray(int *a)

are same

Mike
  • 47,263
  • 29
  • 113
  • 177
Omkant
  • 9,018
  • 8
  • 39
  • 59