字符串问题? [英] String Problem?
问题描述
大家好,
我发现
char x [4] = {" my"};
can编译。
但是
char x [4];
x = {" my"};
无法编译。
为什么?
如有任何建议,我们将不胜感激!
Best问候,
Davy
文章< 11 ********* *************@g44g2000cwa.googlegroups .com> ;,
Davy< zh ******* @ gmail.com>写道:我发现
char x [4] = {" my"};
可以编译。
但是
char x [4];
x = {" my"};
无法编译。
为什么?
因为它是这样的。
变量声明的初始化器有语法可用
在其他地方不可用。
-
相机制造商暂时推迟推出
亚毫巴分辨率生物超维等离子体空间聚合成像,
但指示仍然指日可待。
Ok davy,要了解这个,
你需要一些C / C ++处理数组名称和数组的方式背景
指针。
char * p1 =" hello";
char p2 [] =你好;
现在.. p1和p2可用于大多数地方可互换..喜欢
1. p1 [0];
2. *(p2 + 2)
等等..
但是很少有例外。
1. p2 ++
2. p2 - = 10;
3. p2 = p1
4. char * ch =" world " ;
p2 = ch;
无效
拇指规则是你不能对其进行任何修改操作地址
由数组名称指向。你将得到一个LValue所需的错误。
Davy < ZH ******* @ gmail.com>在消息中写道
news:11 ********************** @ g44g2000cwa.googlegr oups.com ...大家好,
我发现
char x [4] = {" my"};
可以编译。
但是
char x [4];
x = {" my"};
无法编译。
为什么?
任何建议都会非常感谢!
致以诚挚的问候,
Davy
Davy写道:< blockquote class =post_quotes>大家好,
我发现
char x [4] = {" my"};
可以编译。
是的,但最好这样做:
char x [] =" my";
这种方式只有足够的空间用于我的提供。
但是
char x [4];
x = {" my"};
无法编译。
因为x的类型...
char x [4];
....实际上是......
char * const x;
....这意味着指针的值英寸×"是不变的 - 可能不是
修改,而这样做......
x = {" my"}; // - 实际上x =" my" ...
....尝试修改指针x,这就是你得到编译器的原因
错误。
建议如下:
char x [100] = {''\ 0''};
std :: ostrstream os(x,sizeof(x));
os<< Hooh,hah <<的std ::结束; //不要忘记std :: ends;
或...
std :: string x(" my" );
或......
char x [4] = {''\ 0''};
strcpy(x," my");
请记住,数据的内容存在于地址
位置所在的x点,是可以修改的。地址本身(或ptr的
值)不是。
问候,
W
为什么?
任何建议都将不胜感激!
此致,Davy
>
Hi all,
I found
char x[4]={"my"};
can be compiled.
But
char x[4];
x={"my"};
can not be compiled.
Why?
Any suggestions will be appreciated!
Best regards,
Davy
In article <11**********************@g44g2000cwa.googlegroups .com>,
Davy <zh*******@gmail.com> wrote:I found
char x[4]={"my"};
can be compiled. But
char x[4];
x={"my"};
can not be compiled. Why?
Because that''s how it is.
Initializers on a variable declaration have syntaxes available
that are not available in other places.
--
Camera manufacturers have temporarily delayed introduction of
sub-millibarn resolution bio-hyperdimensional plasmatic space polyimaging,
but indications are that is still just around the corner.
Ok davy, to understand this,
you need some background on the way C / C++ deals with array names and array
pointers.
char* p1 = "hello";
char p2[] = hello;
now.. p1 and p2 can be used in most places interchangeable.. like
1. p1[0];
2. *(p2+2)
etc etc..
but there are few exceptioins for this.
1. p2++
2. p2 - =10;
3. p2 = p1
4. char* ch = "world" ;
p2 = ch;
ARE NOT VALID
the thumb rule is "you cant do any modication operations to the address
pointed by the array name". You will get an LValue required error.
"Davy" <zh*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...Hi all,
I found
char x[4]={"my"};
can be compiled.
But
char x[4];
x={"my"};
can not be compiled.
Why?
Any suggestions will be appreciated!
Best regards,
Davy
Davy wrote:Hi all,
I found
char x[4]={"my"};
can be compiled.
Yes, but it is better to do this:
char x[] = "my";
This way just enough space for "my" is provided.
But
char x[4];
x={"my"};
can not be compiled.
Because the type of x in...
char x[4];
....is effectively...
char* const x;
....which means the value of the pointer "x" is constant - may not be
modified, whereas doing this...
x={"my"}; //- actually x = "my"...
....attemps to modify the pointer x, which is why you get a compiler
error.
Suggestions as follow:
char x[100] = { ''\0'' };
std::ostrstream os( x, sizeof(x) );
os << "Hooh, hah" << std::ends; //don''t forget std::ends;
or...
std::string x("my");
or...
char x[4] = { ''\0'' };
strcpy( x, "my" );
Remember, the contents of the array which exists at the address
location whereto x points, is modifiable. The address itself (or the
value of the ptr) is not.
Regards,
W
Why?
Any suggestions will be appreciated!
Best regards,
Davy
这篇关于字符串问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!