Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 表达式必须是可修改的左值C++;?_C++_Arrays_String - Fatal编程技术网

C++ 表达式必须是可修改的左值C++;?

C++ 表达式必须是可修改的左值C++;?,c++,arrays,string,C++,Arrays,String,在C中,我可以将字符串存储为数组,因此我尝试在C++中执行以下操作: message = "I am sending this back"; 其中,消息按以下方式定义: #define mx 100 #define sizeArr (mx+1) 总的来说: char message[sizeArr]; 但我得到以下错误:“表达式必须是可修改的左值。” P> >下面的代码不是你想要的独立于C语言或C++的代码。 char message[100]; message = "abc"; 你必

在C中,我可以将字符串存储为数组,因此我尝试在C++中执行以下操作:

message = "I am sending this back";
其中,消息按以下方式定义:

#define mx 100
#define sizeArr (mx+1)
总的来说:

char message[sizeArr];
但我得到以下错误:“表达式必须是可修改的左值。”


<> P> >下面的代码不是你想要的独立于C语言或C++的代码。
char message[100];
message = "abc";
你必须这样做:

char message[100];
strcpy( message, "abc" );
在C++中有更好的解决方案,只需使用STD::String:

string message;
message = "abc";

如果必须使用字符数组,以下是一个常见的习惯用法:

static const char my_text[] = "This is my text.";
长度的一个常见习惯用法是,因为它是编译时常量:

const size_t length_my_text = sizeof(my_text); // Includes terminating nul character.
对于不带终止nul字符的文本长度:

const size_t length_no_nul = sizeof(my_text) - 1U;
有关复制字符数组的信息,请参见:
strcpy
strcat
strdup
strncpy
,以及
strncat
。另请参见
strlen
strcmp


文本的首选C++习惯用法是使用<代码> STD::String

做<代码> char消息[siZARR];
然后
message=“我正在发回”即使在C语言中也会产生误导,很可能不是您想要的。您不能在.eew宏中为
char
数组分配字符串文字。为什么不使用适当的(<代码> const )变量?@ JePijuHL宏在某些情况下是不EEEE的。@ NICOMP在99 +%的情况下(IHHO),这里肯定是。因为OP标记了C++的一个帖子,一个更好的解决方案是使用<代码> STD::String < /Cord>。strcat和strdup也不是首选。只有在编译时不需要该字符串时,才首选使用
std::string
。对于内存受限或禁止动态内存分配(由于碎片)的安全情况,不首选
std::string