* ptr的[10]和(* PTR)[10]之间差 [英] Difference between *ptr[10] and (*ptr)[10]

查看:131
本文介绍了* ptr的[10]和(* PTR)[10]之间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关以下code:

    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] 按顺序)。

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).

但这个code片段:

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.

推荐答案

为int * PTR [10];

这是10 为int * 指针,而不是你会以为,一个指向10数组 INT 取值

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

INT(* PTR)[10];

这是一个指向10数组 INT

This is a pointer to an array of 10 int

这是我相信一样为int * PTR; 中既可以指向数组,但指定的形式也只能点到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

这篇关于* ptr的[10]和(* PTR)[10]之间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆