strcat的 [英] strcat
问题描述
为什么以下代码会崩溃?
int main()
{
char * str =" aa" ;;
strcat(str," hello");
cout<< str<< " \ n";
strcat(str," you1");
cout<< str<< " \ n";
返回0;
}
< blockquote> Eric Kaplan写道:
为什么以下代码会崩溃?
你的意思是在它之后已被修复编译?
int main()
{
char * str =" aa" ;;
strcat(str," hello");
这是怎么回事? (提示什么是str?)。
-
Ian Collins。
" Eric Kaplan" < to ********* @ yahoo.comwrote in message
news:u0 ********************* *********** @ 4ax.com ...
为什么以下代码会崩溃?
int main()
{
char * str =" aa" ;;
str指向哪里?它指向一个字符串文字。
strcat(str," hello");
str可修改的位置是什么?
< ac ****** @ gmail.com写在留言
4月2日下午4:01,Eric Kaplan< tobycraf ... @ yahoo.comwrote:
>为什么以下代码会崩溃?
int main()
{
char * str =" aa";
strcat(str," hello");
cout<< str<< " \ n";
strcat(str," you1");
cout<< str<< " \ n";
返回0;
}
< snip> ;
2)字符串文字是常量对象,这意味着它们包含的
字符可能无法修改。事实上它是
IIRC,这个说法不对。 C
标准中定义的字符串文字未定义为常量。然而,修改它们会导致未定义的行为。这是我从C89中回忆起来的(我必须承认它已经很长一段时间了,所以我可能错了)。大多数Unix编译器将字符串
文字放在只读区域中,因此修改它们会在那里违反
违规行为。
Sharad
why the following code will crash?
int main()
{
char* str = "aa";
strcat (str, "hello");
cout << str << "\n";
strcat (str, "you1");
cout << str << "\n";
return 0;
}
Eric Kaplan wrote:why the following code will crash?
Do you mean after it has been fixed to compile?
int main()
{
char* str = "aa";
strcat (str, "hello");What is this trying to do? (hint what is str?).
--
Ian Collins.
"Eric Kaplan" <to*********@yahoo.comwrote in message
news:u0********************************@4ax.com...why the following code will crash?
int main()
{
char* str = "aa";Where is str pointing to? It''s pointing to a string literal.
strcat (str, "hello");Is the location pointed to by str modifiable?
<ac******@gmail.comwrote in messageOn Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@yahoo.comwrote:>why the following code will crash?
int main()
{
char* str = "aa";
strcat (str, "hello");
cout << str << "\n";
strcat (str, "you1");
cout << str << "\n";
return 0;
}
<snip>
2) String literals are constant objects, which means that the
characters that they contain may not be modified. The fact that it isIIRC, this statement is not true. String literals as defined in the C
Standard are not defined as constant. Yet, modifying them leads to undefined
behavior. This is what I recollect from C89 (I have to admit that it''s been
quite some time, so I may be wrong). Most of the Unix compilers place string
literals in a read only region, so modifying them gets a segmention
violation there.
Sharad
这篇关于strcat的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!