C 中的运算符优先级 - 返回值 [英] Operator Precedence in C - Returning a Value

查看:36
本文介绍了C 中的运算符优先级 - 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个声明:

return *local_stack_var2++ + 42;

这些是分解时的正确步骤吗:
1.取消引用local_stack_var2
2. 给解引用的local_stack_var2加上42(函数实际上会返回这个值)
3.函数结束前,会激活post-increment,增加local_stack_var2指向的对象的值

Would these be the proper steps when breaking it down:
1. Dereference local_stack_var2
2. Add 42 to the dereferenced local_stack_var2 (function will actually return this value)
3. Before the function is over, it will activate the post-increment, incrementing the value of the object pointed to by local_stack_var2

所以在代码格式中,它看起来像这样?

So in code format, it would look kind of something like this?

int temp = *local_stack_var2 //step 1;  
int returnValue = temp + 42; //step 2, compiler will return THIS value     
*local_stack_var2 = *local_stack_var2 + 1; //step 3 
 return returnValue;

谢谢!

推荐答案

Close,但是++(后增量)比一元*有更高的优先级,所以它先发生.操作顺序为:

Close, but ++ (postincrement) has higher precedence than unary *, so it happens first. The order of operations would be:

  1. 后增量 local_stack_var2 使其加 1,但表达式计算为原始值,而不是增量值
  2. 取消引用原始值
  3. 42 添加到通过取消引用上述原始值获得的内容
  4. 返回那个值
  1. Post increment local_stack_var2 so that it is incremented by one but the expression evaluates to the original value, not the incremented value
  2. Dereference that original value
  3. add 42 to what was obtained by dereferencing the aforementioned original value
  4. return that value

所以在代码中,它会(不精确,但接近)

So in code, it would be like (not precisely, but close)

int* temp = local_stack_var2;
local_stack_var2 = local_stack_var2 + 1;
int retval = *temp;
reval = retval + 42;
return retval;

这篇关于C 中的运算符优先级 - 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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