当你解引用后一个增量时会发生什么C [英] What happens when you dereference a postincrement C

查看:144
本文介绍了当你解引用后一个增量时会发生什么C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到很多关于此的相互矛盾的答案。但是,正如我一直理解的那样。



当我们在C中有一个指针,并在后增量语句中使用它时,后续增量总是会在代码行解析后发生。

  int数组[6] = {0,1,2,3,4,5}; 
int * p = array;
printf(%d,* p ++); //这将输出0,然后递增指针到1

输出:

  0 

非常简单的东西。现在,我在这里收到了人们告诉我的信息和我自己的经历中的一些不和谐。

  //相同的代码如前
int数组[0] = {0,1,2,3,4,5};
int * p = array;
printf(%d,*(p ++)); //发出此行

输出:

  0 

现在当我运行第二个版本的代码结果是它将输出0然后递增指针。括号中隐含的操作顺序似乎被违反。然而,本网站的其他一些答案告诉我,应该发生的适当的事情是增量应该在解除引用之前发生。所以我想我的问题是这样的:我的理解是否正确?增量增量语句是否总是在行尾执行?

附加信息:

我正在使用gcc版本ubuntu 4.8.4在linux mint上编译gcc, p>

我也在debian上使用debian 4.7.2在gcc上测试了这个。

解决方案

OP的结果是,它会输出0,然后增加指针。是不正确的。

后缀增量返回指针的值。将该值视为原始指针值的副本。指针递增,不影响副本。


后缀++运算符的结果是操作数的值。作为副作用,操作数对象的值增加。 ... C11dr 6.5.2.4 2


然后指针的副本被去引用并返回 0 。这是功能序列的事件。



由于增加指针和取消引用指针副本的副作用不会相互影响,哪一个先发生是无关紧要的。编译器可以按照自己的喜好进行优化。

行尾不涉及代码。这是重要的表达式的结尾。


I am receiving a lot of conflicting answers about this. But as I always understood it.

When we have a pointer in C and use it in a post increment statement, the post increment will always happen after the line of code resolves.

int array[6] = {0,1,2,3,4,5};
int* p = array;
printf("%d", *p++); // This will output 0 then increment pointer to 1

output :

0

Very simple stuff. Now here's where I am receiving a bit of dissonance in the information people are telling me and my own experience.

// Same code as Before
int array[0] = {0,1,2,3,4,5};
int* p = array;
printf("%d", *(p++)); // Issue with this line

output :

0

Now when I run that second version of the code The result is that it will output 0 THEN increments the pointer. The order of operations implied by the parentheses seems to be violated. However some other answers on this site tell me that the proper thing that should happen is that the increment should happen before the dereference. So I guess my question is this: Is my understanding correct? Do post increment statements always execute at the end of the line?

Additional Info:

I am compiling with gcc on linux mint with gcc version ubuntu 4.8.4

I have also tested this on gcc on debian with version debian 4.7.2

解决方案

OP's "The result is that it will output 0 THEN increments the pointer." is not correct.

The postfix increment returns the value of the pointer. Consider this value as a copy of the original pointer's value. The pointer is incremented which does not affect the copy.

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented. ... C11dr 6.5.2.4 2

Then the copy of the pointer is de-referenced and returns the 0. That is the functional sequence of events.

Since the side-effect of incrementing the pointer and de-referencing that copy of the pointer do not effect each other, which one occurs first is irrelevant. The compiler may optimized as it likes.

"the end of the line" is not involved in code. It is the end of the expression that is important.

这篇关于当你解引用后一个增量时会发生什么C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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