我有一个 C++ 函数,我知道它是什么,但为什么?
int c[5];
int* pc = c;
for (int i = 0; i < 5; i++)
{
c[i] = i*2;
}
*pc++;
printf("%d\n", 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("%d\n", pc-c ); // will print the pointer differences. 1.
请注意
*pc++;
实际上意味着
*(pc++);
这与
不同(*pc)++;
如有疑问,请始终使用括号。
关于c++ - 这段代码的输出是什么?为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16739130/