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

查看:35
本文介绍了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 的指针,绿色表示指向数组的指针,...)和变量是你可以指向的东西使用正确的激光笔,即您不能使用绿色激光笔指向字符变量.

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];/* 在大多数情况下,a"本身被转换为第一个元素的地址":&a[0] 与 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天全站免登陆