* ptr 和(* ptr)之间的差异

对于以下代码:

int (*ptr)[10]; int a[10]={99,1,2,3,4,5,6,7,8,9}; ptr=&a; printf("%d",(*ptr)[1]); 

它应该打印什么? 我期待这里的垃圾值,但输出为1
(我得出结论,初始化这种方式指针数组,即ptr[10]将按顺序开始指向a[10]元素。

但是这个代码片段怎么样:

 int *ptr[10]; int a[10]={0,1,2,3,4,5,6,7,8,9}; *ptr=a; printf("%d",*ptr[1]); 

它给出了分段错误。

int *ptr[10];

这是一个包含10个int*指针的数组,不像你想象的那样,是一个指向10个int数组的指针

int (*ptr)[10];

这是一个指向10 int数组的指针

我认为和int *ptr; 因为两者都可以指向一个数组,但是给定的forms只能指向一个10个int的数组

 int (*ptr)[10]; 

是一个指向10个int数组的指针。

 int *ptr[10]; 

是一个10指针的数组。

段错的原因:

* PTR = A; 的printf( “%d”,* PTR [1]);

在这里,您将数组a的地址分配给ptr ,该地址将指向元素a[0] 。 这相当于: *ptr=&a[0];

但是,当你打印时,你访问ptr[1]这是一个未初始化的指针,它是未定义的行为,因此给出段错误。

int(*)[10]是一个指向具有10个成员的int数组的指针。 即它指向int a[10]

其中int *[10]是整数指针数组

 #include  int main() { int *ptr[10]; int a[10]={0,1,2,3,4,5,6,7,8,9}; printf("\n%p %p", ptr[0], a); *ptr=a; //ptr[0] is assigned with address of array a. printf("\n%p %p", ptr[0], a); //gives you same address printf("\n%d",*ptr[0]); //Prints zero. If *ptr[1] is given then *(ptr + 1) ie ptr[1] is considered which is uninitialized one. return 0; } 

int(* p)[10]表示p现在是指向大小为10的整数数组的指针。

int * p [10]表示p是10个整数指针的数组。

int (*p)[10]是指向每行中10个整数数组的指针,即可以有任意数量的行。 基本上它可以用来指向一个二维数组,维度可以通过递增i来获取*(p+i)[10] ,这与a[i][10] ,这里'i=1,2,3...'访问1,2,3.. rows

其中as, int *p[10]是一个包含10个整数指针的数组。

例如,如果数组是两个维数

b[2][10]={{0,1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18,19}}; 基本上, (*ptr)[10] // where the higher dimension index is omitted ie, 2可以用作指向数组的二维指针(每行包含10个元素,即第一维),如(*ptr)[10] = &b 。 在printf("%d",(*ptr)[1]); 如提供的(*ptr)*(ptr+0)相同,则计算第一个维度,即b[0][0] ,其值为0.就像明智一样,访问数组的第二维b[1][0]表达式为**(ptr+1)*(ptr+1)[0]*(*(ptr+i)+j); // where i=1 and j=0 *(*(ptr+i)+j); // where i=1 and j=0给出第二维的第一个元素,即10。

我已经回答了很久,所以很容易理解。

(*ptr)[10]的唯一两个用例是:

  • 使用二维数组时;
  • typedef固定长度数组。

由于上面已经由Sree harsha Punyasamudram解释了第一个案例,我将解释更具异国情调的用例。

  #include  typedef int arr1d10[10]; typedef int arr2d3[3][3]; void test1d0(arr1d10 *arr) { /* This is the same as the case with function test1d1(). */ printf("Element: %d\n", (*arr)[0]); } void test1d1(int (*arr)[10]) { /* As arr is a pointer to an array of 10 integers, pointer arithmetic will work with the size of an array of 10 integers, ie when you increment arr it will increment by sizeof(arr1d10), which is 40 bytes. That's why when you dereference it, you can't simply do arr[1], because it will increment arr by 40 bytes forward. Also when dereferencing it, because it thinks it points to a whole array it will give you that array - it's address. This is another reason you can't do just arr[i], because those will be just addresses. The correct way is to dereference it once(*arr)), to get the address (think of implicitely casting to int*) and then you can do as usually (*arr)[1]). */ printf("Element: %d\n", (*arr)[1]); } void test2d0(arr2d3 *arr2d) { /* This is a little more complicated, but the principle is the same as above. */ printf("Element: %d\n", (*arr2d)[0][0]); } void test2d1(int (*arr2d)[3][3]) { printf("Element: %d\n", (*arr2d)[0][1]); } int main(void) { arr1d10 arr1d = {0, 1, 2}; arr2d3 arr2d = { {0,1},{2,3},{3,4}}; int (*p1d)[10] = &arr1d; int (*p2d)[3][3] = &arr2d; printf("********** PRINT 1D array **********\n"); test1d0(&arr1d); test1d1(&arr1d); test1d0(p1d); test1d1(p1d); printf("********** PRINT 2D array **********\n"); test2d0(&arr2d); test2d1(&arr2d); test2d0(p2d); test2d1(p2d); return 0; }