这段代码的输出是什么?
What is the out put of this code any why?
我在c++中有一个函数,我知道会是什么,但为什么?
int c[5];
int* pc = c;
for (int i = 0; i < 5; i++)
{
c[i] = i*2;
}
*pc++;
printf("%dn", pc-c );
有很多垃圾代码在运行。这是打印的唯一重要的事情:
int c[5]; // c is a pointer
int* pc = c; // pc points to the same thing as c.
pc++; // pc now points to one-past-where-c-points-to
printf("%dn", pc-c ); // will print the pointer differences. 1.
注意
*pc++;
实际上意味着
*(pc++);
不同于
(*pc)++;
地址距离
似乎代码试图显示内存寻址空间中指针pc
到c
的距离。
-
int* pc = c;
:pc
指向c
所指向的地方。(这里pc = c
) -
*pc++;
:pc
增加1(此处为pc = c + 1
) -
pc - c
:pc - c
=1
: 距离(它们之间的整数数)
,
+------+------+------+------+------+
| | | | | |
+------+------+------+------+------+
^ ^
c pc
您可以阅读定义良好的指针算术[expr]。
输出为1
,因为它减去地址并返回差值
相关文章:
- 为对象分配整数.输出将是什么?
- 以下与指针相关的代码的输出是什么?
- 从 c++ 中的实例化进程获取输出的可靠方法是什么?
- 我的输出结果是 0 英寸C++.可能是什么问题
- 以下程序在 c++ 中的输出是什么?
- 任何人都解释下面的代码,它的输出是什么?
- 这段代码的输出是什么,它在构造函数中使用 strcpy?
- 以下代码的输出是什么(答案因差异编译器而异)
- 该代码将如何工作以及输出的输出是什么
- 这里发生了什么,还有输出是什么?(无线索)
- 此代码的输出是什么?我在这里错过了什么吗?
- 假设动态范围,以下C++程序的输出是什么
- "cout << (a, b)"的输出是什么,为什么?
- ".hidden"输出对象转储 -t 的输出是什么意思?
- c中的fork()命令——这段简单代码的输出是什么
- 例外C++,我只是不明白为什么输出是什么
- 以下代码的输出是什么?
- 这段代码的输出是什么?
- 这个Valgrind输出是什么意思?
- 下面代码的输出是什么?