C Initialization of Pointer Matrices

Im pretty confused that the difference between these two initializations is:

    int (*p)[10];

and

    int *p[10]

I know that both of them can point to a 2D array whose element count in the string is 10 ....

+5
source share
6 answers

To elaborate on the correct answers here:

First line:

 int (*p)[10];

declares that "p" is a pointer to a memory address of an array with a capacity of 10 int. It can be read in English like this: "integer-pointer" p "points to 10 consecutive ints in memory."

Second line:

int *p[10]

, "p []" 10 . , . "p" 10 ( ints).

+2

- , - .

+4
int (*p)[10];


+------+                           +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|  p   | =========================>|(*p)[0]|(*p)[1]|(*p)[2]|(*p)[3]|(*p)[4]|(*p)[5]|(*p)[6]|(*p)[7]|(*p)[8]|(*p)[9]|
+------+                           +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

sizeof p sizeof (void *) (4 32- , 8 64- )

sizeof *p 10*sizeof (int) (40 )

int *p[10];   is the same as     int* (p[10]);   

p
+------+------+------+------+------+------+------+------+------+------+
| p[0] | p[1] | p[2] | p[3] | p[4] | p[5] | p[6] | p[7] | p[8] | p[9] |
+------+------+------+------+------+------+------+------+------+------+

sizeof p 10*sizeof (void *) (40 32- , 80 64- )

sizeof *p sizeof (int *) (4 32- , 8 64- )

+2

p 10 . p 10 .

" int" .

int (**ptr)[10] = new (int (*)[10]);

. - ints; .

10 :

int *ptr = new int[10];

, ( ), 10 int int (*)[10]. , new, typedef, ; .

, new[] , , ( ) .

, 10 int. , , , ( 1) .

int (*p)[10] = new int[1][10];

1 ( 10 int), delete[] p.

delete[] p;

, .

+1

( ) p ; , p 10 .

0

, int (*p)[10] p 10- int, int *p[10] p 10- int,

C , . , , .

, p - int ( ). , ,

x = *p[i];

Postfix, [] (), , , *, *(p[i]) (IOW, * p[i]),

*p[i] int, p int *p[10];.

, p int ( ). , ,

x = (*p)[i];

Again, since it []has a higher priority than *, we need to use parentheses to explicitly associate *only with p, and not p[i]. The type of expression (*p)[i]is equal int, so the declaration pis equal int (*p)[10];.

0
source

All Articles