我需要一些基本的C ++帮助 [英] I need some basic C++ help

查看:72
本文介绍了我需要一些基本的C ++帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串变量声明为char'的数组。

代码看起来像这样。


char name [80];


然后当我尝试使用变量它不起作用,但我不是

确定你可以按照我想要的方式使用它。有人可以告诉我,我是做错了什么,或者是另一种做同样事情的方法。我是

试图使用这样的变量。


name =" modem";

感谢您的任何帮助可以给我。

I am trying to declare a string variable as an array of char''s. the
code looks like this.

char name[80];

then when i try to use the variable it dosn''t work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.

推荐答案



" Psykarrd" < PS ****** @ hotmail.com>在消息中写道

新闻:19 ************************** @ posting.google.c om ...

"Psykarrd" <ps******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
我试图将字符串变量声明为char'的数组。
代码看起来像这样。

char name [80];

然后当我尝试使用变量时它不起作用,不过我是不确定你可以按照我想要的方式使用它。有人可以告诉我,我做错了什么,或者做同样事情的另一种方式。我正在尝试使用这样的变量。

name =" modem";

感谢您提供给我的任何帮助。
I am trying to declare a string variable as an array of char''s. the
code looks like this.

char name[80];

then when i try to use the variable it dosn''t work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.




strcpy(name," modem");


表达式,例如modem等编译器将其视为const char *

(指向内存中字符串文字modem存储的结果为

)。


所以,你给出的表达式是试图为数组分配一个地址。由于

这样的操作没有意义,你的编译器大喊大叫!



strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!


Psykarrd写道:
Psykarrd wrote:
我试图将字符串变量声明为char'的数组。
代码看起来像这样。

char name [80];

然后当我尝试使用变量时它不起作用,不过我是不确定你可以按照我想要的方式使用它。有人可以告诉我,我做错了什么,或者做同样事情的另一种方式。我正在尝试使用这样的变量。

name =" modem";
...
I am trying to declare a string variable as an array of char''s. the
code looks like this.

char name[80];

then when i try to use the variable it dosn''t work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
...




''name''和modem"是_arrays_。 C ++中的数组是不可分配的。

为了将一个数组复制到另一个数组,一般情况下你必须复制

他们手动。 - 逐个元素。好消息是,在大多数

的情况下,有一个库功能可以为你做。


在这种特殊情况下你可以使用'' strcpy'':


strcpy(姓名,调制解调器);


-

祝你好运,

Andrey Tarasevich



Both ''name'' and "modems" are _arrays_. Arrays in C++ are not assignable.
In order to copy one array to another in general case you have to copy
them "manually" - element by element. The good news is that in most
cases there''s a library function that can do it for you.

In this particular case you can use ''strcpy'':

strcpy(name, modem);

--
Best regards,
Andrey Tarasevich




" Dave"其中p是*********** @ yahoo.com>在消息中写道

news:vr ************ @ news.supernews.com ...

"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com...

Psykarrd ; < PS ****** @ hotmail.com>在消息中写道
新闻:19 ************************** @ posting.google.c om ...

"Psykarrd" <ps******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
我试图将字符串变量声明为char'的数组。
代码看起来像这样。

char name [80];

然后当我尝试使用变量时它不起作用,不过我是不确定你可以按照我想要的方式使用它。有人可以告诉我,我做错了什么,或者做同样事情的另一种方式。我正在尝试使用这样的变量。

name =" modem";

感谢您提供给我的任何帮助。
I am trying to declare a string variable as an array of char''s. the
code looks like this.

char name[80];

then when i try to use the variable it dosn''t work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.



strcpy(name," modem");

表达式,例如modem等。被编译器视为const char *
(指向存储器中字符串文字modem存储的地址)。

所以,表达式你给了试图给阵列分配一个地址。由于这样的操作没有意义,你的编译器大喊大叫!



strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!




哎呀,对不起,我应该说数组 of const char而不是const char

*。所以,它实际上是一个数组,然后*然后*当编译器遇到调制解调器时,该数组会经历一个指针的转换

。最后,正如我之前说的那样,
之前,指针无法分配给数组......



Oops, sorry, I should have said "array of const char" instead of "const char
*". So, it''s really an array, and *then* that array undergoes a conversion
to a pointer when the compiler encounters "modem". Finally then, as I said
before, a pointer cannot be assigned to an array...


这篇关于我需要一些基本的C ++帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆