Code:
#include <stdio.h>
void foo( int *a )
{
printf("a is : %p\n", (void *)a );
printf("&a is : %p\n", (void *)&a );
printf("&a[0] is: %p\n", (void *)&a[0] );
}
void bar( int a[] )
{
printf("a is : %p\n", (void *)a );
printf("&a is : %p\n", (void *)&a );
printf("&a[0] is: %p\n", (void *)&a[0] );
}
int main ( void )
{
int a[5] = {0}, *b = a;
printf("In main:\n");
printf("a is : %p\n", (void *)a );
printf("&a is : %p\n", (void *)&a );
printf("&a[0] is: %p\n", (void *)&a[0] );
printf("\n");
printf("In foo(a):\n");
foo( a );
printf("\n");
printf("In bar(a):\n");
bar( a );
printf("\n");
printf("In foo(b):\n");
foo( b );
printf("\n");
printf("In bar(b):\n");
bar( b );
printf("\n");
return 0;
}
In main:
a is : 0xbffffce0
&a is : 0xbffffce0
&a[0] is: 0xbffffce0
In foo(a):
a is : 0xbffffce0
&a is : 0xbffffcc0
&a[0] is: 0xbffffce0
In bar(a):
a is : 0xbffffce0
&a is : 0xbffffcc0
&a[0] is: 0xbffffce0
In foo(b):
a is : 0xbffffce0
&a is : 0xbffffcc0
&a[0] is: 0xbffffce0
In bar(b):
a is : 0xbffffce0
&a is : 0xbffffcc0
&a[0] is: 0xbffffce0
The name of an array is in effect a pointer to its first element. You'll see that in every example here. What changes is the address of the array or pointer itself. When passed to a function, "array[]" is changed to "*array", or, a pointer to that type. This is illustrated by the functions
foo and
bar.
The only thing different in the output is as it should be: The address of the pointer itself is different then what you get inside
main. The reason is, it's a different pointer. If you'll recall, if you can, a pointer is a variable that stores an address. Thus, the pointer itself has an address. Since it is an actual different variable, it has a (shock!)
a different address. This you'll see as the second line in the output of functions
foo and
bar.
Quzah.