它是否正确? [英] Is this correct?
问题描述
如果我写的话
char * p =" hello world" ;;
在C中是正确的还是我必须分配内存块然后
strcpy字符串到指针。
如果在C中不正确,在C ++中是允许的,我在一些c ++书中看到了这样的语法;
谢谢
lee
-
通过Mailgate.ORG服务器发布 - http: //www.Mailgate.ORG
推荐答案
2003年12月16日星期二05:53:01 +0000(UTC)," ;杨利
< ya ******* @ ausi.com>在comp.lang.c中写道:
On Tue, 16 Dec 2003 05:53:01 +0000 (UTC), "Yang Lee"
<ya*******@ausi.com> wrote in comp.lang.c:
如果我写的话
char * p =" hello world" ;这是正确的C或者我必须分配内存块然后
strcpy字符串到指针。
如果在C中不正确,是吗?在C ++中允许,我在一些c ++书中看到了这样的语法;
谢谢
lee
Hi,
If I write
char *p="hello world";
is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.
If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;
thanks
lee
这是在C和C ++中都是正确的。
指针p被初始化为指向
未命名的字符数组的第一个字符,其中包含字符串文字。 br />
在C ++中,此数组的类型是常量char数组。在C中,
语言没有指定字符串文字是否为常量或
不是。但是在这两种语言中,它都是未定义的行为,你试图用
修改字符串文字。
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq
This is correct in both C and C++.
The pointer p is initialized to point to the first character of the
unnamed array of characters holding the string literal.
In C++, the type of this array is array of constant char. In C the
language does not specify whether the string literal is constant or
not. But in both languages, it is undefined behavior is you try to
modify the string literal.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Lee,
On Tue,2003年12月16日05:53:01 UTC,Yang Lee <亚******* @ ausi.com>写道:
Lee,
On Tue, 16 Dec 2003 05:53:01 UTC, "Yang Lee" <ya*******@ausi.com> wrote:
如果我写的话
char * p =" hello world" ;;
在C中是正确的还是我必须分配内存块然后
strcpy字符串到指针。
这在C和C ++中是正确的。
引用的文本(hello world)将存储在progams内存
空间。指针p将指向内存中的该字符串。
(hello world)内存块的大小可能是12个字节。
如果你只想将它用作常量或字符串空间
小于原始内存块就可以了。如果你在这个p的定义中复制一个更大的字符串,你将覆盖
你不知道它是什么内存的某些部分。那可能是
危险。
如果在C中不正确,在C ++中是否允许,我在一些c ++书籍中看到了这样的语法;
谢谢
lee
Hi,
If I write
char *p="hello world";
is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.
This is correct in C and C++.
The quoted text ("hello world") will be stored in the progams memory
space. The pointer p will point to this string in memory.
The size of the ("hello world") memory block is likely to be 12 bytes.
If you just want this to be used as a constant or for a string space
that is less than the original memory block that is okay. If you were
to copy a larger string over this definition of p, you would overwrite
some part of memory that you do not know what it is. That can be
dangerous.
If not correct in C , is it allowable in C++ , i seen such syntax in
some books of c++;
thanks
lee
David
David
David写道:
David wrote:
Lee,
在星期二,2003年12月16日05:53:01 UTC,Yang Lee <亚******* @ ausi.com>写道:
Lee,
On Tue, 16 Dec 2003 05:53:01 UTC, "Yang Lee" <ya*******@ausi.com> wrote:
如果我写了
char * p =" hello world";
在C中是正确的还是我必须分配内存块然后
strcpy字符串到指针。
Hi,
If I write
char *p="hello world";
is this correct in C or do i have to assign memory block and then
strcpy the string to pointer.
这在C和C中是正确的C ++。
引用的文本(hello world)将存储在progams内存空间中。指针p将指向内存中的该字符串。
(hello world)内存块的大小可能是12个字节。
如果你只是想要这个用作常量或字符串空间
小于原始内存块即可。如果你是在这个p的定义上复制一个更大的字符串,你会覆盖你不知道它是什么的一部分内存。这可能是危险的。
This is correct in C and C++.
The quoted text ("hello world") will be stored in the progams memory
space. The pointer p will point to this string in memory.
The size of the ("hello world") memory block is likely to be 12 bytes.
If you just want this to be used as a constant or for a string space
that is less than the original memory block that is okay. If you were
to copy a larger string over this definition of p, you would overwrite
some part of memory that you do not know what it is. That can be
dangerous.
这是明显错误的。任何修改字符串文字的尝试都会导致未定义的行为导致
。新字符串
是否比旧字符串更短或更长并不重要。未定义未定义。
-Kevin
-
我的电子邮件地址有效,但会定期更改。
如需联系我,请使用最近发布的地址。
This is blatantly wrong. Any attempt to modify a string literal results
in undefined behavior. It doesn''t matter at all whether the new string
is shorter or longer than the old. Undefined is undefined.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
这篇关于它是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!