int(* ptr)[4]的真正含义是什么,它与* ptr有何不同? [英] What int (*ptr)[4] really means and how is it different than *ptr?

查看:255
本文介绍了int(* ptr)[4]的真正含义是什么,它与* ptr有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int (*p)[4] , *ptr;
int a[4] = {10,20,30,40};
printf("%p\n%p\n%p",&a,a,&a[0]);
p = &a ;
//p=a;        gives error

//ptr = &a;   gives error
 ptr = a;

输出:

0x7ffd69f14710
0x7ffd69f14710
0x7ffd69f14710

我试图了解a&a&a[0]返回的内容及其起始变量的内存地址.那么,为什么在其中一些作业中出现错误?

I tried to understand what a, &a, and &a[0] returns and its the memory address of starting variable. So, why am I getting errors in some of these assignments ?

我的意思是,如果p = &a = 0x7ff...有效,为什么p = a = 0x7ff..不起作用?

I mean, if p = &a = 0x7ff... works, why not p = a = 0x7ff.. ?

如果可能的话,请让任何人通过框图了解该p和ptr实际指向的位置.还是他们只是指向相同.但是我肯定知道它们是不同的东西.

If possible, can anyone please make me understand through a block diagram of where this p and ptr is actually pointing to too. Or are they just pointing same. But they're different things that for sure I know.

推荐答案

想象一下,指针是激光指针,具有不同的颜色(红色是指向int的指针,绿色是指向数组的指针,...),变量是可以指向的对象使用正确的激光指示器,即,您不能使用绿色的激光指示器指向char变量.

Imagine pointers are laser pointers, with different colors (red for pointers to int, green for pointers to arrays, ...) and variables are things you can point to with the correct laser pointer, ie, you cannot use a green laser pointer to point to a char variable.

好,所以您有一个int a[4]数组(4个整数).使用绿色指针指向它:int (*green)[4] = &a; ...您还具有一个int(a[0]),可以使用红色指针指向它:int *red = &a[0]; /* in most contexts 'a' by itself is converted to "address of first element": &a[0] is the same as a */.

Ok, so you have int a[4] an array (of 4 ints). Use a green pointer to point to it: int (*green)[4] = &a;... you also have an int (a[0]) which you can point to with a red pointer: int *red = &a[0]; /* in most contexts 'a' by itself is converted to "address of first element": &a[0] is the same as a */.

现在问问您的色盲朋友,指针指向::
就您的朋友而言,他们是平等的,并指向同一个地方" ...但是您欺骗了您的朋友! 编译器是色盲的,不喜欢被欺骗.

Now ask your color-blind friend where the pointers point to :)
As far as your friend is concerned, they are equal and point to the same "place"... but you tricked your friend! Compilers are color-blind and don't like being tricked.

这篇关于int(* ptr)[4]的真正含义是什么,它与* ptr有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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