0

Why I'm getting compiling error in following code? and What is the difference between int (*p)[4], int *p[4], and int *(p)[4]?

#include <stdio.h>
int main(){
   int (*p)[4];// what is the difference between int (*p)[4],int *p[4], and int *(p)[4]
   int x=0;
   int y=1;
   int z=2;
   p[0]=&x;
   p[1]=&y;
   p[2]=&z;
   for(int i=0;i<3;++i){
      printf("%i\n",*p[i]);
   }
   return 0;
}
1
  • 1
    Bookmark this page: cdecl.org it translates a C declaration to English Commented Jan 29, 2016 at 17:51

3 Answers 3

2

There is no difference between

int *p[4];

and

int *(p)[4];

Both declare p to be an array of 4 pointers.

int x;
p[0] = &x;

is valid for both.

int (*p)[4];

declares p to be a pointer to an array of 4 ints.

You can get more details on the difference between

int *p[4];
int (*p)[4];

at C pointer to array/array of pointers disambiguation.

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

Comments

1
  • int (*p)[4]: (*p) is an array of 4 int => p pointer to an array (array of 4 int)
  • int *p[4] = int * p[4]: p is an array of 4 int *
  • int *(p)[4]: same as the second

In your case, you should the second form.

Comments

1

This is array of 4 pointers: int *p[4]. So array will have 4 pointers, they point to int value. This is the same int *(p)[4].

As for int (*p)[4]; this is pointer to an array of 4 integers.

Comments

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.