2016-01-29 103 views
0

为什么我得到在下面的代码编译错误?是什么int (*p)[4]int *p[4]int *(p)[4]之间的区别?C编程。指针数组和指针数组

#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

收藏本页:http://www.cdecl.org/其转换为C声明英语 – Barmar

回答

2

int *p[4]; 

int *(p)[4]; 

之间没有区别两者声明p是的4个指针的阵列。

int x; 
p[0] = &x; 

对两者均有效。

int (*p)[4]; 

声明p是一个指针4 int秒的阵列。

你可以在C pointer to array/array of pointers disambiguation得到更多的细节上的差异

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

1
  • int (*p)[4](*p)是4 int =>p指针的阵列到阵列
  • int *p[4] = int * p[4](4 int阵列):p是4 int *
  • int *(p)[4]的数组:相同第二

在你的情况,你应该第二种形式。

1

这是4个指针数组:int *p[4]。所以数组将有4个指针,它们指向int值。这与int *(p)[4]相同。

作为INT (*p)[4];这是指针4个整数的数组。