Почему значения a и a отличаются для массива, переданного как параметр функции?b и & b не отличаются для массива, определенного в теле функции.Код следующий:
void foo(int a[2])
{
int b[2];
printf("%p %p\n", a, &a);
printf("%p %p\n", b, &b);
}
int main()
{
int a[2];
foo(a);
return 0;
}
РЕДАКТИРОВАТЬ:
Итак, после всего обсуждения, я понимаю, что происходит следующее:
В main()
:
int a[2]; /* define an array. */
foo(a); /* 'a' decays into a pointer to a[0] of type (int*). */
/* since C is pass-by-value, this pointer is replicated and */
/* a local copy of it is stored on the stack for use by foo(). */
In foo()
:
printf("%p %p\n", a, &a); /* 'a' is the value of the pointer that has been replicated, */
/* and it points to 'a[0]' in main() */
/* '&a' is the address of the replicated pointer on the stack. */
/* since the stack grows from higher to lower addresses, */
/* the value of '&a' is always lower than a. */