What is the difference between `a` and` * a`, where `a` denotes a 2D array?

Pointers and arrays are closely related to each other, so if we have a 2-dimensional array

int a[3][4]={
             1,2,3,4,
             5,6,7,8,
             9,10,11,12};

Both

printf("%p\n", a);

and

printf("%p\n", *a);

type the same address. I understand that it probably apoints to the base address a, but *apoints to the first submatrix of a two-dimensional array a.

So what is the difference between the two?

+4
source share
5 answers

Given an ad

int a[3][4];

the following is true:

Expression     Type            Decays to        Value
----------     ----            ---------        -----
         a     int [3][4]      int (*)[4]       Base address of array
        &a     int (*)[3][4]   n/a              Base address of array
        *a     int [4]         int *            Base address of first subarray
                                                (equivalent to a[0])
      a[i]     int [4]         int *            Base address of i'th subarray
     &a[i]     int (*)[4]      n/a              Base address of i'th subarray
     *a[i]     int             n/a              Value of a[i][0]
   a[i][j]     int             n/a              Value of a[i][j]
  &a[i][j]     int *           n/a              Address of a[i][j]

, a, &a, *a, a[0], &a[0] &a[0][0] ( ), (int (*)[3][4] vs. int (*)[4] vs. int *).

Edit

, sizeof &, "N- T" ( "" ) "" T ", .

, *a a[0]; "4- int". sizeof &, "", " int".

, "".

. 6.3.2.1 - C 2011 .

+4

, , a, while * a a.

.

  • a ( int(*)[3]) , printf.
  • *a subarray a ( int[3]) ( int*) .

, , , .

+5

a - 2D- 3x4. :

  • sizeof(a) == 3*4*sizeof(int)
  • a[i] 1D- 4 ( 0 <= < 3)
  • a[i][j] int (0 <= < 3 0 <= j < 4)
  • a &a[0]

*a a[0]: 1D 4. :

  • sizeof(*a) == 4*sizeof(int)
  • (*a)[i] - int (0 <= < 4)
  • *a &a[0][0]

So a *a - , , &a. a int[4], *a int ( undur_gongor ).

+3

( ) , .

, . a , *a int s.

, . . a + 1 *a + 1.

+1

. *a - . *a , [1,2,3,4]. (*a)[i], i - .

a . a[0] ( , *a), a[1] ( [5,6,7,8]).

For example, if you want to access the 5th element (which is 5), it will be a[1][0].

0
source

All Articles