C语言 *ptr[10] 和 (*ptr)[10] 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13910749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:46:21  来源:igfitidea点击:

Difference between *ptr[10] and (*ptr)[10]

cpointers

提问by Amit Gupta

For the following code:

对于以下代码:

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

What should it print? I'm expecting the garbage value here but the output is 1.
(for which I'm concluding that initializing this way pointer array i.e ptr[10]would start pointing to elements of a[10]in order).

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

But what about this code fragment:

但是这个代码片段呢:

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

It is giving the segmentation fault.

它给出了分段错误。

采纳答案by Karthik T

int *ptr[10];

int *ptr[10];

This is an array of 10 int*pointers, not as you would assume, a pointer to an array of 10 ints

这是一个包含 10 个int*指针的数组,而不是您所假设的,指向包含 10 个ints的数组的指针

int (*ptr)[10];

int (*ptr)[10];

This is a pointer to an array of 10 int

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

It is I believe the same as int *ptr;in that both can point to an array, but the given form can ONLY point to an array of 10 ints

我相信int *ptr;两者都可以指向一个数组,但给定的形式只能指向一个 10int秒的数组

回答by P.P

int (*ptr)[10];

is a pointer to an array of 10 ints.

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

int *ptr[10];

is an array of 10 pointers.

是一个包含 10 个指针的数组。

Reason for segfault:

段错误的原因:

*ptr=a; printf("%d",*ptr[1]);

*ptr=a; printf("%d",*ptr[1]);

Here you are assigning the address of array ato ptrwhich would point to the element a[0]. This is equivalent to: *ptr=&a[0];

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

However, when you print, you access ptr[1]which is an uninitialized pointer which is undefined behaviour and thus giving segfault.

但是,当您打印时,您访问的ptr[1]是一个未初始化的指针,该指针是未定义的行为,因此会产生段错误。

回答by Jeyaram

int(*)[10]is a pointer to an int array with 10 members. i.e it points to int a[10].

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

where as int *[10]is array of integer pointers

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

#include <stdio.h>
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) i.e ptr[1] is considered which is uninitialized one.

return 0;
}

回答by Sree harsha Punyasamudram

int (*p)[10]is a pointer to an array of 10 integers in each row i.e there can be any number of rows. basically it can be used to point to a 2D array and the dimensions can be accessed by incrementing ifor *(p+i)[10]which is same as a[i][10], here 'i=1,2,3...'to access 1,2,3.. rows.

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

where as, int *p[10]is an array of 10 integer pointers.

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

If array is two dimesional i.e, for example

如果数组是二维的,例如

b[2][10]={{0,1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18,19}};basically, (*ptr)[10] // where the higher dimension index is omitted i.e, 2can be used as two dimensional pointer to an array(containing 10 elements in each row i.e, the 1st dimension) like (*ptr)[10] = &b. In the printf("%d",(*ptr)[1]);as provided (*ptr)is same as *(ptr+0)evaluates to the first dimension i.e, b[0][0]which value is 0. like wise, to access the 2nd dimension of the array b[1][0]the expression will be **(ptr+1)or *(ptr+1)[0]or *(*(ptr+i)+j); // where i=1 and j=0gives the first element of the 2nd dimension i.e, 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 i.e, 2可以用作指向数组(每行包含 10 个元素,即第一维)的二维指针,例如(*ptr)[10] = &b. 在printf("%d",(*ptr)[1]);as 提供(*ptr)的与*(ptr+0)评估第一维相同,即b[0][0]该值为 0。同样,要访问数组b[1][0]的第二维,表达式将是**(ptr+1)*(ptr+1)[0]*(*(ptr+i)+j); // where i=1 and j=0给出第二维的第一个元素,即 10。

I've answered it long so to understand it easy.

我已经回答了很长时间,所以很容易理解。

回答by малин чекуров

The only two use cases for (*ptr)[10]are:

仅有的两个用例(*ptr)[10]是:

  • when working with two-dimensional arrays;
  • typedef fixed length arrays.
  • 处理二维数组时;
  • typedef 定长数组。

Since the first case was already explained above by Sree harsha Punyasamudram, I am going to explain the more exotic use case.

由于第一个案例已经由 Sreeharsha Punyasamudram 在上面解释过,我将解释更奇特的用例。

    #include <stdio.h>

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, i.e. 
       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;
}

回答by wanttomasterpython

int (*p)[10] means that p is now a pointer to an integer array of size 10.

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

int *p[10] means that p is an array of 10 integer pointers .

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