关于\0的问题 [英] Question on \0

查看:57
本文介绍了关于\0的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char * s =''\ 0''

if(* s ==''\'''){

printf(" yes" ;);

}


上述代码是否可以正常工作?

解决方案

cr*******@gmail.com 说:


char * s =''\ 0''

if(* s ==''\'''){

printf(是);

}


上述代码是否可以正常运行?



不,它甚至不会编译。鉴于它只是一个代码片段,我们将会b / b忽略缺乏家具。但是第一个

线上缺少分号是不可能的,是吗?此外,虽然在这种特殊情况下,

值''\ 0''实际上/工作/作为指针的初始化(因为

它只是一个int值为0),会更合适。另外,

你会想要制作const,以防止意外写作

它。并且你需要确保你的文本输出流是正确的

用换行符终止:


#include< stdio.h>


int main(无效)

{

const char * s ="" ;;

if (* s ==''\''')

{

printf(" yes \ n");

}


返回0;

}


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


< blockquote class =post_quotes>
char * s =''\ 0''

if(* s ==''\'''){

printf(是);

}



好​​对不起,忘记了分号。我很抱歉,如果你看到

普通代码添加结尾''\ n''和main()并且
$ b $不准确b什么不是 - 我在概念上要求更多,而不是实际使用

代码。我正在查看给我的

项目的框架代码,我相信我发现了一个bug,所以我想问一个

简化的问题,而不是给整个部分。


cr ******* @ gmail.com 写道:


char * s =''\'''



如果用分号终止上述语句,则它将指针s初始化为空指针。在这里,''\'''只是

另一种写作方式0.


请注意,它不会初始化s指向空字符串。要做

,你应该写:


const char * s ="" ;;


我使用const来提醒你字符串文字是不可写的。


如果你真的想要一个恰好包含

空字符串的可写缓冲区:


char s [100] ="" ;;


您必须指定缓冲区的大小。一个100字符缓冲区可以保存一个长达99个字符的
字符串,最后允许一个字节用于null

字符。


if(* s ==''\'''){



这里你取消引用一个空指针。这可能会导致您的

程序崩溃。无论如何,它是未定义的行为。


printf(" yes");

}


上述代码是否可以正常运行?



No.


-

Simon。


char * s = ''\0''
if(*s==''\0''){
printf("yes");
}

Should the above code work without error?

解决方案

cr*******@gmail.com said:

char * s = ''\0''
if(*s==''\0''){
printf("yes");
}

Should the above code work without error?

No, it won''t even compile. Given that it''s just a code fragment, we''ll
overlook the lack of "furniture". But the lack of a semicolon on the first
line is not promising, is it? Also, although in this particular case the
value ''\0'' will actually /work/ as an initialisation for a pointer (because
it is simply an int with the value 0), "" would be more appropriate. Also,
you''ll want to make s const, to guard against accidentally writing through
it. And you will want to ensure that your text output stream is properly
terminated with a newline character:

#include <stdio.h>

int main(void)
{
const char *s = "";
if(*s == ''\0'')
{
printf("yes\n");
}

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


char * s = ''\0''
if(*s==''\0''){
printf("yes");
}

Okay sorry, forgot the semicolon. I''m sorry if it hurts you to see
plain code that''s not precise in adding an ending ''\n'' and main() and
what not--I was asking more conceptually rather than for actual use of
that code. I was looking through the framework code given to me for my
project and I believe I discovered a bug so I wanted to ask a
simplified question rather than give the whole piece.


cr*******@gmail.com wrote:

char * s = ''\0''

If you terminate the above statement with a semicolon, then it
initialises the pointer s to be a null pointer. Here, ''\0'' is just
another way of writing 0.

Note that it does not initialise s to point to an empty string. To do
that, you should write:

const char *s = "";

I made it const to remind you that string literals are not writable.

If you actually want a writable buffer that just happens to contain an
empty string:

char s[100] = "";

You have to specify a size for the buffer. A 100 char buffer can hold a
string up to 99 characters long, allowing one byte for the null
character at the end.

if(*s==''\0''){

Here you are dereferencing a null pointer. This is likely to cause your
program to crash. At any rate, it is undefined behaviour.

printf("yes");
}

Should the above code work without error?

No.

--
Simon.


这篇关于关于\0的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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