Originally Posted by
jeanluca
but why does this work:
Code:
int array[6]= { 1, 2, 3, 4, 5, 6 };
int len=sizeof(array)/sizeof(int);
array is a pointer too!
An array is not a pointer.
The wrinkle is that, in some contexts, the name of an array can be used as if it is a pointer.
If you do this;
Code:
#include <stdio.h>
int main()
{
int array[] ={ 1, 2, 3, 4, 5, 6 };
int *pointer = array; /* in this context array is a pointer */
fprintf(stdout, "sizeof(array)/sizeof(int) %u\n", sizeof(array)/sizeof(int)); /* will print 6 */
fprintf(stdout, "sizeof(pointer)/sizeof(int) %u\n", sizeof(pointer)/sizeof(int)); /* will print something else */
}
The size of a pointer (which is simply a variable that contains a memory address) is compiler-dependent. The size of a pointer doesn't change if the pointer happens to contain the address of the first element of an array.