C pointer array

When creating an array of pointers in c, what does the effect of adding parentheses do?

for instance

int (*poi)[2];

against

int *poi[2];

+6
source share
3 answers

Pointer to an array of 2 int s:

 int (*poi)[2]; 

An array of two int pointers:

 int *poi[2]; 

A normal array takes precedence over a pointer, but if you add parentheses, the pointer comes "first."

+8
source

The index operator [] bound more strongly than the warp operator * .

 int *poi[2] 

translates to:

If you see poi , apply [x] to it, then search for the result with * and get int . So this is an array of 2 pointers to int.

IN

 int (*poi)[2] 

brackets force * be applied first. Therefore, at any time, poi is used if you first use * and then [x] get an int . So this is a pointer to an array of 2 int .

+2
source

The braces are snapped stronger than *, so the first is an array of int pointers, and the second is a pointer to an array of int.

0
source

Source: https://habr.com/ru/post/906445/


All Articles