- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试学习 C 中的指针,并为此参加了一个测验。这是问题:
#include <stdio.h>
char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;
int main()
{
printf("%s ", **++cpp);
printf("%s ", *--*++cpp+3);
printf("%s ", *cpp[-2]+3);
printf("%s ", cpp[-1][-1]+1);
return 0;
}
行的结果:
printf("%s ", *cpp[-2]+3);
让我感到困惑,但让我逐步解释一下我是如何理解的。
char *c[]
- 是指向 char 的指针数组。char **cp[]
- 是指向 char 指针的指针数组(我认为这是 *c[]< 的包装器
倒序)。char ***cpp
- 是指向指向 char 的指针的指针(我认为这是 **cp[]< 的包装器
执行位置修改)。**++cpp
- 由于 cpp
指向 cp
,那么 ++cpp
将指向cp+1
即 c+2
,因此双重解引用将打印 TEST
。
*--*++cpp+3
- 因为现在 cpp
指向 cp+1
,那么 ++cpp
会指向cp+2
也就是c+1
,接下来的操作--
会给我们指向c
,所以最后一个取消引用将打印 sQuiz
。
困惑来了:
cpp[-2]
- 因为现在 cpp
指向 cp+2
,我可以确认
printf("%p\n", cpp); // 0x601090
printf("%p\n", cp+2); // 0x601090
这里我打印c
中的指针地址
printf("c - %p\n", c); // c - 0x601060
printf("c+1 - %p\n", c+1); // c+1 - 0x601068
printf("c+2 - %p\n", c+2); // c+2 - 0x601070
printf("c+3 - %p\n", c+3); // c+3 - 0x601078
所以当我像这样取消引用时 *(cpp[0])
或 **cpp
我期望得到 MCQ
的值 c+1
printf("%p\n", &*(cpp[0])); // 0x601068
但是当我说 *(cpp[-2])
时,我得到了 QUIZ
,但我更希望得到一些垃圾值。
所以我的问题是:
*--*++cpp+3
的魔法是如何工作的,我的意思是 --
部分修改了什么让我得到MCQ
而不是 TEST
当我像这样取消引用 **cpp
时,我假设这个指针 *++cpp+3
在应用 --
后保留状态,但还无法想象它是如何工作的。
为什么下面的代码会这样工作(cpp[-2]
部分):
printf("%p\n", &*cpp[1]); // 0x601060 -> c
printf("%p\n", &*(cpp[0])); // 0x601068 -> c+1
printf("%p\n", &*(cpp[-1])); // 0x601070 -> c+2
printf("%p", &*(cpp[-2])); // 0x601078 -> c+3
它似乎有相反的顺序,我可以接受 &*(cpp[0])
指向 c+1
,但我希望 & *cpp[1]
指向c+2
,&*(cpp[-1])
指向c
。我在这个问题中发现:Are negative array indexes allowed in C?
最佳答案
首先让我澄清一下负面索引的混淆,因为我们稍后会用它来回答其他问题:
when I say
*(cpp[-2])
I getQUIZ
, but I would rather expect to get some garbage value.
负值没问题。 Note the following :
By definition, the subscript operator
E1[E2]
is exactly identical to*((E1)+(E2))
.
知道了,因为 cpp == cp+2
,那么:
cpp[-2] == *(cpp-2) == *(cp+2-2) == *cp == c+3
因此:
*cpp[-2]+3 == *(c+3)+3 == c[3]+3
这意味着 "QUIZ"
的地址加上 char
指针的 3 个位置,所以你传递给 printf
的地址"QUIZ"
中的字符 Z
,这意味着它将从那里开始打印字符串。
实际上,如果您想知道,-2[cpp]
也是等效且有效的。
现在,问题:
- How the magic with
*--*++cpp+3
works, I mean what is modified by the -- part that allows me to getMCQ
instead ofTEST
when I dereference like this**cpp
, I assume that this pointer*++cpp+3
preserves the state after the -- is applied, but cannot imagine yet how it works.
让我们分解一下(回想一下 cpp == cp+1
,正如您正确指出的那样):
++cpp // cpp+1 == cp+2 (and saving this new value in cpp)
*++cpp // *(cp+2) == cp[2]
--*++cpp // cp[2]-1 == c (and saving this new value in cp[2])
*--*++cpp // *c
*--*++cpp+3 // *c+3
正如您正确指出的那样,它指向 sQuiz
。但是,cpp
和 cp[2]
已修改,因此您现在拥有:
cp[] == {c+3, c+2, c, c}
cpp == cp+2
cp[2]
已更改这一事实并未在问题的其余部分中使用,但请注意这一点很重要——特别是因为您打印了指针的值。见:
Why the following works the way it works (the
cpp[-2]
part):printf("%p\n", &*cpp[1]); // 0x601060 -> c
printf("%p\n", &*(cpp[0])); // 0x601068 -> c+1
printf("%p\n", &*(cpp[-1])); // 0x601070 -> c+2
printf("%p", &*(cpp[-2])); // 0x601078 -> c+3
首先,让我们将&*x
简化为x
。然后,执行与上面类似的操作,如果 cpp == cp+2
(如上),您可以看到:
cpp[ 1] == cp[3] == c
cpp[ 0] == cp[2] == c // Note this is different to what you had
cpp[-1] == cp[1] == c+2
cpp[-2] == cp[0] == c+3
- I obviously confuse many things, and may call something a pointer that in reality is not one, I would like to grasp the concept of pointer, so will be glad if someone show me where I am wrong.
其实你学得很好! :-)
基本上,指针是一个表示内存地址的整数。但是,当您对其执行算术运算时,它会考虑它指向的类型的大小。这就是为什么如果 c == 0x601060
和 sizeof(char*) == 8
,那么:
c+1 == 0x601060 + 1*sizeof(char*) == 0x601068 // Instead of 0x601061
c+2 == 0x601060 + 2*sizeof(char*) == 0x601070 // Instead of 0x601062
关于c - 了解 C 指针、数组和负索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50535008/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!