0

I am trying to understand the logic between the three writing: array, &array and &array[0]:

#include <stdio.h>

void foo(int *a);
void bar(int (*a)[3]);
void baz(int a[3]);

int main(void) {
    int array[] = {1,2,3};

    int* a = array;
    int (*b)[sizeof(array)/sizeof(array[0])] = &array;
    int* c = &array[0];

    printf("%p, %p, %p", a, b, c);
}

I always thought int a[] is a kind-of int *. And in fact it works:

int *p = array;

But the address of a pointer to an int is int**:

int *u;
int **v = &u;

However here, the adresse of array is not int** but int (*)[3]. I am lost.

What does the standard says about &array?

nowox
  • 25,978
  • 39
  • 143
  • 293
  • Lose the notion "`int a[]` is a kind-of `int *`" and then there is no confusion. The array of ints is several single `int`s next to each other in memory. It has an address just like any other variable – M.M Aug 22 '19 at 07:06
  • I would accept that, but what is precisely `int a[]` then ? – nowox Aug 22 '19 at 07:07
  • dupe of : https://stackoverflow.com/questions/54807208/why-are-array-and-array-pointing-to-the-same-address – selbie Aug 22 '19 at 07:09
  • 1
    more info: https://stackoverflow.com/questions/33775701/different-pointer-arithmetic-results-when-taking-address-of-array – selbie Aug 22 '19 at 07:10
  • @nowox an array, which I explained in my first comment – M.M Aug 22 '19 at 07:15

0 Answers0