关于优先级的疑问, 请教, 谢谢

bulala 2011-07-26 10:27:01
看一个答案里有
char *strcpy(char *strDest, const char *strSrc);
{
assert((strDest!=NULL) && (strSrc !=NULL)); // 2分
char *address = strDest; // 2分
while( (*strDest++ = * strSrc++) != ‘\0’ ) // 2分
NULL ;
return address ; // 2分
}
其中while 这行 ,是如何计算的?
先取* 再赋值,再++ 再取*strSrc, strsrc再++ ?
*strDest++ 是应该先计算++再取值吧 ?

...全文
415 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
风吹PP凉SS 2011-07-28
  • 打赏
  • 举报
回复
+1[Quote=引用 1 楼 bdmh 的回复:]
先进行 *strDest = * strSrc,后,在执行++
[/Quote]
topspark 2011-07-28
  • 打赏
  • 举报
回复
先做后置++,再做解引用*,c++ primer 147页上有操作符的优先级。后置++ > 前置++ = 解引用*
qq69696698 2011-07-27
  • 打赏
  • 举报
回复


先进行 *strDest = * strSrc,后,在执行++
bulala 2011-07-26
  • 打赏
  • 举报
回复
如果是这样的话, 那每次调用函数拷贝时,不是就漏掉了第一个吗?
但我实验的并没有漏啊
[Quote=引用 16 楼 luuillu 的回复:]
赋值是最后执行的。根据优先级和结合性加括号后得:
(*(strDest++)) = (* (strSrc++))

先分别计算出等号两端的表达是的值,然后赋值。
[/Quote]
luuillu 2011-07-26
  • 打赏
  • 举报
回复
赋值是最后执行的。根据优先级和结合性加括号后得:
(*(strDest++)) = (* (strSrc++))

先分别计算出等号两端的表达是的值,然后赋值。
赵4老师 2011-07-26
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。

不要写连自己也预测不了结果的代码!
Jxiaoshen 2011-07-26
  • 打赏
  • 举报
回复
(*strDest++ = * strSrc++)
*strDest++ 表示*(strDest++) ,所以先是*(strDest++) = * (strSrc++ )
即*strDest= * strSrc 然后再strDest++ , strSrc++


cwh5635 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 echoyin59 的回复:]

这个其实就相当于:
第一步先赋值:*strDest = *strSrc;
第二步:实现++
[/Quote]
ls你說錯了哦。看我代碼。。。然後自己看書去
starl1985 2011-07-26
  • 打赏
  • 举报
回复
先取值,在做++操作,楼主可以调试自己看下
cwh5635 2011-07-26
  • 打赏
  • 举报
回复

class testoperator
{
int value_;
public:
testoperator()
{
this->value_ = 0;
}
testoperator& operator ++(int)
{
int temp = this->value_++;
cout<<"++"<<endl;
return *this;
}
int operator *()
{

cout<<"*"<<endl;
return this->value_;//test operator priority
}
};
int main()
{
testoperator xxoo;
*xxoo++;
}

自己真相吧
诶呦 2011-07-26
  • 打赏
  • 举报
回复
这个其实就相当于:
第一步先赋值:*strDest = *strSrc;
第二步:实现++
bulala 2011-07-26
  • 打赏
  • 举报
回复
左边的*src++我能够理解, 但是到了 *src++=本来++的优先级高, 为何还要先做=呢?
ipyan 2011-07-26
  • 打赏
  • 举报
回复
以后分不清,还是带上()吧,清晰明了
赵4老师 2011-07-26
  • 打赏
  • 举报
回复
VC调试时按Alt+8,TC或BC用TD调试,打开汇编窗口看每句C对应的汇编并单步执行一遍不就啥都明白了吗。
(Linux或Unix下应该也可以在用GDB调试时,看每句C对应的汇编并单步执行。)
赵4老师 2011-07-26
  • 打赏
  • 举报
回复
// Operator Precedence and Associativity
// The table below lists the C and C++ operators and their precedence and associativity values. The highest precedence level is at the top of the table.
// Symbol |Name or Meaning |Associativity
// |Highest Precedence |
// ++ |Post-increment |Left to right
// -- |Post-decrement |
// ( ) |Function call |
// [ ] |Array element |
// -> |Pointer to structure member |
// . |Structure or union member |
// ++ |Pre-increment |Right to left
// -- |Pre-decrement |
// ! |Logical NOT |
// ~ |Bitwise NOT |
// - |Unary minus |
// + |Unary plus |
// & |Address |
// * |Indirection |
// sizeof |Size in bytes |
// new |Allocate program memory |
// delete |Deallocate program memory |
// (type) |Type cast [for example, (float) i]|
// .* |Pointer to member (objects) |Left to right
// ->* |Pointer to member (pointers) |
// * |Multiply |Left to right
// / |Divide |
// % |Remainder |
// + |Add |Left to right
// - |Subtract |
// << |Left shift |Left to right
// >> |Right shift |
// < |Less than |Left to right
// <= |Less than or equal to |
// > |Greater than |
// >= |Greater than or equal to |
// == |Equal |Left to right
// != |Not equal |
// & |Bitwise AND |Left to right
// ^ |Bitwise exclusive OR |Left to right
// | |Bitwise OR |Left to right
// && |Logical AND |Left to right
// || |Logical OR |Left to right
// ? : |Conditional |Right to left
// = |Assignment |Right to left
// *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=|Compound assignment |
// , |Comma |Left to right
// |Lowest Precedence |
至善者善之敌 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
先进行 *strDest = * strSrc,后,在执行++
[/Quote]

++1,这个昨天讨论过了,看结合性,而非优先级
BorrowedStory 2011-07-26
  • 打赏
  • 举报
回复
建议去看一下操作符重载 看到前置后后置++\--就一目了然了
Michael_Xie 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]

先进行 *strDest = * strSrc,后,在执行++
[/Quote]
+1
BorrowedStory 2011-07-26
  • 打赏
  • 举报
回复

应该是先执行++
++的优先级要高一些
由于这里的 后置++,所以结果类似于先执行 *

后置++ 或-- 的执行那个过程

temp = param;
param = param + 1;
return temp;

也就是将参数加一,再返回原来的数据


http://www.cppblog.com/aqazero/archive/2006/06/08/8284.html
bdmh 2011-07-26
  • 打赏
  • 举报
回复
先进行 *strDest = * strSrc,后,在执行++
加载更多回复(6)

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧