'const char'和'char'之间的区别 [英] Differences between 'const char' and 'char'
问题描述
这两个陈述有什么区别?是否有任何主要的
差异?
const char filename [] =" ips_c.txt";
char filename [ ] =" ips_c.txt";
谢谢!!!
hokieghal99< ho ******** @ hotmail.com>写在
新闻:bl ********* @ solaris.cc.vt.edu:
这两者有什么区别声明?是否有任何重大差异?
const char filename [] =" ips_c.txt" ;;
char filename [] =" ips_c.txt";
只有编译时差。如果你试图写入文件名的第一个
定义,就像在文件名[0] =''我'中那样,你将得到一个
编译时错误。为第二个定义做同样的事情,你
将没有错误,文件名[]将包含Ips_c.txt。
off-topic :有些实现可能会将const版本放入某种类型的只读内存中。
-
- Mark - >
-
hokieghal99< ho ******** @ hotmail.com>写道:
这两个陈述有什么区别?是否有任何重大差异?
const char filename [] =" ips_c.txt" ;;
char filename [] =" ips_c.txt";
首先,这些是声明,而不是声明[*]。第二,是的,
存在差异:尝试修改前文件名'
会产生未定义的行为。允许C实现
将前文件名置于只读状态。内存部分。
[*]在C99中,声明可以是声明,但它仍然是误导的b $ b。
- -
"这将是未定义行为的一个更好的例子
如果行为未定义。
- 迈克尔鲁宾斯坦
" Mark A. Odell" <无**** @ embeddedfw.com>写道:
hokieghal99< ho ******** @ hotmail.com>在
新闻中写道:bl ********* @ solaris.cc.vt.edu:
这两个陈述有什么区别?是否有任何重大的差异?
const char filename [] =" ips_c.txt";
char filename [] =" ips_c.txt";
只有编译时差。如果您尝试写入文件名的第一个
定义,如filename [0] =''我',您将收到编译时错误。为第二个定义做同样的事情,你将没有错误,文件名[]将包含Ips_c.txt。
它可能是与你的C
实现只有编译时差异,但标准没有说明这一点。
偏离主题:某些实现可能会放置const版本在某种情况下只读内存。
因此,可能存在运行时差异。
-
事实上,有一场圣战并不意味着其中一方
并不吸吮 - 通常两者都做...... />
- 亚历山大·维罗
What is the difference between these two statements? Are there any major
differences?
const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";
Thanks!!!
hokieghal99 <ho********@hotmail.com> wrote in
news:bl*********@solaris.cc.vt.edu:
What is the difference between these two statements? Are there any major
differences?
const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";
Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = ''I'' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".
off-topic: some implementations may place the const version into some sort
of read-only memory.
--
- Mark ->
--
hokieghal99 <ho********@hotmail.com> writes:
What is the difference between these two statements? Are there any
major differences?
const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";
First, these are declarations, not statements[*]. Second, yes,
there is a difference: attempting to modify the former `filename''
yields undefined behavior. The C implementation is allowed to
place the former `filename'' in a "read-only" section of memory.
[*] In C99, a declaration can be a statement, but it''s still
misleading.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
"Mark A. Odell" <no****@embeddedfw.com> writes:
hokieghal99 <ho********@hotmail.com> wrote in
news:bl*********@solaris.cc.vt.edu:What is the difference between these two statements? Are there any major
differences?
const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";
Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = ''I'' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".
It may be only a compile-time difference with your C
implementation, but the standard doesn''t say anything about that.
off-topic: some implementations may place the const version into some sort
of read-only memory.
Thus, there may be a runtime difference.
--
"The fact that there is a holy war doesn''t mean that one of the sides
doesn''t suck - usually both do..."
--Alexander Viro
这篇关于'const char'和'char'之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!