指向const字符串的指针 [英] pointer to const string
问题描述
大家好,
我是初学者,我的问题是:
char * p =&之间是否有任何区别; Hello,world!" ;;
和
const char * p =" Hello,world!";
?
//更多信息
如果我定义p& p_const函数如下:
char * p =" Hello,world!";
const char * p_const =" Hello,world!" ;;
似乎TI的编译器不允许
p_const [0] =''A'';
但是它允许
p [0] =''A'';
而MSVC6.0并不允许它们两个。
什么关于C标准?
感谢您的任何建议!
2006年12月1日23:54:56 -0800,死循环 < fo ************ @ gmail.com>
写道:
> //更多信息
如果我定义p& p_const函数如下:
char * p =" Hello,world!";
const char * p_const =" Hello,world!";
似乎是一个TI的编译器不允许
p_const [0] =''A'';
但它允许
p [0] =''A'';
并且MSVC6.0不允许它们两个。
C标准怎么样?
http://www.c- faq.com/ansi/strlitnotconst.html
comp.lang.c常见问题列表问题11.8b
问:如果可以''修改字符串文字,为什么它们不被定义为
是const字符数组?
答:一个原因是代码中包含如此多的代码像
char * p =" Hello,world!" ;;
这些不一定是不正确的。这些行会受到
诊断消息的影响,但它实际上是后来尝试修改
p指向的问题。
>
另见问题1.32。
-
jay
< blockquote> Dead Loop写道:
//更多信息
如果我定义p& p_const函数如下:
char * p =" Hello,world!";
const char * p_const =" Hello,world!" ;;
似乎TI的编译器不允许
p_const [0] =''A'';
但是它允许
p [0] =''A'';
而MSVC6.0并不允许它们两个。
什么关于C标准?
修改字符串文字会导致未定义的行为。
Hi all,
I''m a beginner and my question is:
Are there any differences between
char *p = "Hello, world!";
and
const char *p = "Hello, world!";
?
// more information
If I define p & p_const in a function as follows:
char *p = "Hello, world!";
const char *p_const = "Hello, world!";
It seems that a TI''s compiler doesn''t allow
p_const[0] = ''A'';
but it allows
p[0] = ''A'';
And MSVC6.0 doesn''t allow both of them.
What about the C standard?
Thanks for any advice!
On 1 Dec 2006 23:54:56 -0800, "Dead Loop" <fo************@gmail.com>
wrote:
>// more information
If I define p & p_const in a function as follows:
char *p = "Hello, world!";
const char *p_const = "Hello, world!";
It seems that a TI''s compiler doesn''t allow
p_const[0] = ''A'';
but it allows
p[0] = ''A'';
And MSVC6.0 doesn''t allow both of them.
What about the C standard?http://www.c-faq.com/ansi/strlitnotconst.html
comp.lang.c FAQ list Question 11.8b
Q: If you can''t modify string literals, why aren''t they defined as
being arrays of const characters?
A: One reason is that so very much code contains lines like
char *p = "Hello, world!";
which are not necessarily incorrect. These lines would suffer the
diagnostic messages, but it''s really any later attempt to modify what
p points to which would be problems.
See also question 1.32.
--
jay
Dead Loop wrote:// more information
If I define p & p_const in a function as follows:
char *p = "Hello, world!";
const char *p_const = "Hello, world!";
It seems that a TI''s compiler doesn''t allow
p_const[0] = ''A'';
but it allows
p[0] = ''A'';
And MSVC6.0 doesn''t allow both of them.
What about the C standard?Modifying string literals leads to undefined behaviour.
这篇关于指向const字符串的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!