违反序列点? [英] Violating sequence point?

查看:46
本文介绍了违反序列点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个将字符串转换为全部大写的示例函数:


#include< assert.h>

#include< ctype.h>


void StringUp(char * p)

{

do assert( * p> = 0);

while(* p = toupper(* p),* p ++);

}

请问序列点规则如果代码更改为

,则违反以下内容:

#include< assert.h>

#include< ctype.h> ;


void StringUp(char * p)

{

do assert(* p> = 0);

while(* p ++ = toupper(* p));

}

-


弗雷德里克Gotham


Here''s a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
--

Frederick Gotham

推荐答案

2006-07-05,Frederick Gotham< fg ******* @ SPAM.comwrote:
On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:

>

这是一个将字符串转换为全部大写的示例函数:


#include < assert.h>

#include< ctype.h>


void StringUp(char * p)

{

断言(* p> = 0);

while(* p = toupper(* p),* p ++);

}
>
Here''s a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}



为什么断言()存在?我觉得没用。为什么要这样做?

这条线没有用。

Why is that assert() there? I can see no use for it. Why is the do there?
There''s no use for any of that line.


>

是否会出现序列点规则。如果代码更改为

,则违反以下内容:


#include< assert.h>

#include< ; ctype.h>


void StringUp(char * p)

{

do assert(* p> = 0 );

while(* p ++ = toupper(* p));

}
>
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}



我认为没关系。它仍然是最好的清楚:

while(* p = toupper((unsigned char)* p))

* p ++;


虽然IMO的风格很糟糕,但没有歧义。


-

Andrew Poelstra< http: //www.wpsoftware.net/projects/>

要给我发电子邮件,请使用apoelstra。在上面的地址。

你们人讨厌数学。 - James Harris

I think that''s okay. It''s still best to be clear:
while (*p = toupper ((unsigned char) *p))
*p++;

While that''s terrible style IMO, there''s no ambiguity.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris


2006-07-05,Andrew Poelstra< ap ******* @ wpsoftware.netwrote:
On 2006-07-05, Andrew Poelstra <ap*******@wpsoftware.netwrote:

2006-07-05,Frederick Gotham< fg ******* @ SPAM.comwrote:
On 2006-07-05, Frederick Gotham <fg*******@SPAM.comwrote:

> ;>
这是一个将字符串转换为全部大写的示例函数:

#include< assert.h>
#include< ctype.h>

void StringUp(char * p)
{断言(* p> = 0);
while(* p = toupper(* p),* p ++);
}
>>
Here''s a sample function which converts a string to all uppercase:

#include <assert.h>
#include <ctype.h>

void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}



为什么断言()呢?我觉得没用。为什么要这样做?

这条线没有用。

Why is that assert() there? I can see no use for it. Why is the do there?
There''s no use for any of that line.



没关系;我读了你的另一篇文章解释你的逻辑。你应该

可能会在那里发表评论。


(对于其他人的好处,测试是因为施放到unsigned char更多

一个bandaid而非解决方案。)


-

Andrew Poelstra< http://www.wpsoftware.net/projects/ >

要给我发电子邮件,请使用apoelstra。在上面的地址。

你们人讨厌数学。 - James Harris

Never mind; I read your other post explaining your logic. You should
probably put a comment in there.

(For others'' benefit, the test is because casting to unsigned char is more
of a bandaid than a solution.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris


Frederick Gotham< fg ******* @ SPAM.com撰写:
Frederick Gotham <fg*******@SPAM.comwrites:

是否会出现序列点规则。如果代码更改为

,则违反以下内容:
Would the "Sequence point rule" be violated if the code were changed to the
following:



[...]

[...]


void StringUp(char * p)

{

do assert(* p> = 0);

while (* p ++ = toupper(* p));

}
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}



是。

修改p(in * p ++)和使用(in * p)之间没有序列点。虽然

函数调用包含一个序列点,但编译器可以选择

来评估赋值的左侧和函数

参数之前调用函数调用。


此外,toupper可能(通常是)实现为一个宏

,它不包含与等效函数相同的序列点

功能确实。

-

事实上,有一场圣战并不意味着其中一方

并不傻 - 通常两者都做......

- 亚历山大·维罗

Yes. There is no sequence point intervening between the
modification of p (in *p++) and its use (in *p). Although a
function call contains a sequence point, the compiler may elect
to evaluate the left side of the assignment and the function
argument before invoking the function call.

Furthermore, toupper may be (and often is) implemented as a macro
that does not contain the same sequence point that the equivalent
function does.
--
"The fact that there is a holy war doesn''t mean that one of the sides
doesn''t suck - usually both do..."
--Alexander Viro


这篇关于违反序列点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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