恒定的串怀疑 [英] constant string doubt
问题描述
为什么以下程序会产生运行时错误,而不是
编译错误。任何人请放弃
一些光。
谢谢
sinbad
- ----------------------------
int main()
{
char * a =" abcdefgh" ;;
a [1] =''j'';
printf("%s", a);
}
------------------------------ ----
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.
thanks
sinbad
------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = ''j'';
printf("%s",a);
}
----------------------------------
推荐答案
sinbad< sinbad.sin ... @ gmail.comwrote:
sinbad <sinbad.sin...@gmail.comwrote:
hi,
为什么以下程序会产生运行时错误,
而不是编译错误。任何人请放弃
一些光。
谢谢
sinbad
- ----------------------------
int main()
{
char * a =" abcdefgh" ;;
a [1] =''j'';
printf("%s", a);}
----------------------------------
why does the following program gives an runtime error ,
instead of compilation error. anyone please shed
some light.
thanks
sinbad
------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = ''j'';
printf("%s",a);}
----------------------------------
因为您没有阅读常见问题解答...
http://c-faq.com/decl/strlitinit.html
-
Peter
Because you failed to read the FAQ...
http://c-faq.com/decl/strlitinit.html
--
Peter
2007年10月15日星期一18:33:52 -0700,sinbad< si ***** ******:gmail.com>
在comp.lang.c中写道:
On Mon, 15 Oct 2007 18:33:52 -0700, sinbad <si***********@gmail.com>
wrote in comp.lang.c:
hi,
为什么以下程序给出了运行时错误,而不是
编译错误。任何人请放弃
一些光。
谢谢
sinbad
- ----------------------------
int main()
{
char * a =" abcdefgh" ;;
a [1] =''j'';
printf("%s", a);
}
------------------------------ ----
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.
thanks
sinbad
------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = ''j'';
printf("%s",a);
}
----------------------------------
你的程序会产生两种不同类型的未定义行为。
首先你调用variadic函数printf( )没有原型在
范围内,未定义的行为。
其次你试图修改一个字符串文字的元素,这是
特别是未定义的行为,如C标准所述。
编译器永远不需要为未定义的
行为发出诊断。
< br $> b $ b -
Jack Klein
主页: http://JK-Technology.Com
常见问题解答for
comp.lang.c http://c-faq.com /
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ ajo / docs / FAQ-acllc.html
Your program produces two different types of undefined behavior.
First you call the variadic function printf() without a prototype in
scope, undefined behavior.
Second you attempt to modify an element of a string literal, which is
specifically undefined behavior as stated by the C standard.
A compiler is never required to issue a diagnostic for undefined
behavior.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
10月15日下午6:33,sinbad< sinbad.sin ... @ gmail.comwrote :
On Oct 15, 6:33 pm, sinbad <sinbad.sin...@gmail.comwrote:
hi,
为什么以下程序会给出运行时错误,而不是
编译错误。任何人请放弃
一些光。
谢谢
sinbad
- ----------------------------
int main()
{
char * a =" abcdefgh" ;;
a [1] =''j'';
printf("%s", a);}
----------------------------------
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.
thanks
sinbad
------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = ''j'';
printf("%s",a);}
----------------------------------
''''是指向常量
数据的非常量指针的实例abcdefgh。
所以,您不能使用指针更改数据。
''a'' is an instance of non-constant pointer to the constant
data"abcdefgh".
so,you are not allowed to change the data using pointers.
这篇关于恒定的串怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!