6

When creating a pointer array in c what does the effect of adding parentheses do?

For example

int (*poi)[2];

vs

int *poi[2];

1
  • Saves wear and tear on the keyboard Commented Jan 20, 2012 at 3:10

3 Answers 3

8

Pointer to an array of 2 ints:

int (*poi)[2];

An array of two int pointers:

int *poi[2];

Normally Array has higher precedence than the pointer, but if you add the parentheses then the pointer comes "first".

Sign up to request clarification or add additional context in comments.

2 Comments

.. really? [] is binding tighter I don't see why is this
@LynnYiLu - Just the way C standard is defined.
2

The index operator [] binds stronger than the derefentiation operator *.

int *poi[2]

translates to:

If you see poi, apply [x] to it, then dereference the result via * and you get an int. So it's an array of 2 pointers to int.

In

int (*poi)[2]

the parantheses force the * to be applied first. So anytime poi is used, if you apply * first, and then [x] you get an int. So it's a pointer to an array of 2 int.

Comments

0

Brackets bind tighter than *, so the first is an array of int pointers, while the second is a pointer to an array of ints.

1 Comment

You have that back to front, don't you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.