char数组未以\0结尾 [英] char array not terminated with \0

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

问题描述

嗨。

假设str定义如下

char str [12] =" 012345678901";


使用诸如

printf("%s",str)之类的函数是错误的

strcmp(str2,str)

strcpy( dest,str)


(因为str是一系列字符而没有发送\ 0)?

后果会是什么?


../Anders

Hi.
Suppose str is defined as follows
char str[12] = "012345678901";

Is it wrong to use functions such as
printf("%s",str)
strcmp(str2,str)
strcpy(dest,str)

(since str is an array of chars without the teminating \0)?
What could the consequence be?

../Anders

推荐答案

Anders Christensen写道:
Anders Christensen wrote:
嗨。
假设str的定义如下
char str [12] =" 012345678901";

使用
printf("%s")这样的函数是错误的, str)
strcmp(str2,str)
strcpy(dest,str)


是的。毕竟,它会在哪里停止?
(因为str是一系列字符而没有终止\0)?
结果会是什么?
Hi.
Suppose str is defined as follows
char str[12] = "012345678901";

Is it wrong to use functions such as
printf("%s",str)
strcmp(str2,str)
strcpy(dest,str)
Yes. After all, where would it stop?
(since str is an array of chars without the teminating \0)?
What could the consequence be?




因为它调用未定义的行为,结果可能是*任何*。

如果你碰巧在运行死神站9000 ...


;-)

HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot .com (新帖子12/5)
http:// www .cafepress.com / goldsays



As it invokes undefined behavior, the consequence could be *anything*.
And if you happen to be running a Death Station 9000...

;-)

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


Anders Christensen写道:
Anders Christensen wrote:
嗨。
假设str定义如下
char str [12] =" 012345678901";

使用
printf("%s",str)等函数是不对的
strcmp( str2,str)
strcpy(dest ,str)


是的。

(因为str是一系列字符而没有发送\0)?


正确。

后果可能是什么?
Hi.
Suppose str is defined as follows
char str[12] = "012345678901";

Is it wrong to use functions such as
printf("%s",str)
strcmp(str2,str)
strcpy(dest,str)
Yes.
(since str is an array of chars without the teminating \0)?
Right.
What could the consequence be?




在这些功能时超越缓冲区寻找''\0'',导致

rhinodaemons。



Overrunning the buffer while those functions look for the ''\0'', causing
rhinodaemons.


Martin Ambuhl写道:
Martin Ambuhl wrote:
Anders Christensen写道:
Anders Christensen wrote:
嗨。
假设str定义如下
char str [12] =" 012345678901" ;;

使用
printf("%s" str)等函数是不对的
strcmp(str2,str)
strcpy(dest,str)
Hi.
Suppose str is defined as follows
char str[12] = "012345678901";

Is it wrong to use functions such as
printf("%s",str)
strcmp(str2,str)
strcpy(dest,str)



是的。


Yes.

(因为str是一个没有teminating \0的字符数组)?
(since str is an array of chars without the teminating \0)?



是的。


Right.

后果是什么?



当这些函数查找''\0''时,超越缓冲区,导致
rhinodaemons。


Overrunning the buffer while those functions look for the ''\0'', causing
rhinodaemons.



恶魔,该死的,*恶魔* !!!


守护进程 OT在这里。


- $

-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12 / 5)
http://www.cafepress.com/goldsays


Demons, dammit, *demons*!!!

"Daemons" are OT here.

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


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

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