C语言 无法修改字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14396202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:04:53  来源:igfitidea点击:

Cannot modify char array

carraysmemory-managementcompiler-errorsmplab

提问by newbie

Consider the following code.

考虑以下代码。

char message[]="foo";

void main(void){
    message[] = "bar";
}

Why is there a syntax error in MPLAB IDE v8.63? I am just trying to change the value of character array.

为什么 MPLAB IDE v8.63 中存在语法错误?我只是想改变字符数组的值。

回答by Yakov Shklarov

Assignments like

作业如

message[] = "bar";

or

或者

message = "bar";

are not supported by C.

C 不支持。

The reason the initial assignment works is that it's actually array initialization masquerading as assignment. The compiler interprets

初始赋值起作用的原因是它实际上是伪装成赋值的数组初始化。编译器解释

char message[]="foo";

as

作为

char message[4] = {'f', 'o', 'o', '
message = "bar";
'};

There is actually no string literal "foo"involved here.

这里实际上没有"foo"涉及字符串文字。

But when you try to

但是当你尝试

message[0] = 'b';
message[1] = 'a';

The "bar" is interpreted as an actual string literal, and not only that, but messageis not a modifiable lvalue, ie. you can't assign stuff to it. If you want to modify your array you must do it character by character:

“bar”被解释为一个实际的字符串文字,不仅如此,而且message不是一个可修改的左值,即。你不能给它分配东西。如果要修改数组,则必须逐个字符地进行:

strcpy(message, "bar");

etc, or (better) use a library function that does it for you, like strcpy().

等等,或者(更好)使用一个为你做这件事的库函数,比如 strcpy()。

回答by Rohit Jain

You cannot use character array like that after declaration. If you want to assign new value to your character array, you can do it like this: -

您不能在声明后使用这样的字符数组。如果要为字符数组分配新值,可以这样做:-

message[] = "bar";

回答by MOHAMED

you can do that only in the initialisation when you declare the char array

您只能在声明 char 数组时在初始化中执行此操作

strcpy(message, "bar");

You can not do it in your code

你不能在你的代码中做到这一点

To modify it you can use strcpyfrom <string.h>

要修改它,您可以使用strcpyfrom<string.h>

strcpy(message,"bar");

回答by Tushar Mishra

You cant change the character array like this . If you want to change the value of character array then you have to change it by modifying single character or you can use

您不能像这样更改字符数组。如果你想改变字符数组的值,那么你必须通过修改单个字符来改变它,或者你可以使用

char message[]="foo";

回答by woryzower

char* message="foo"

This statement cause compiler to create memory space of 4 char variable.Starting address of this memory cluster is pointer value of message. address of messageis unchangeable, you cannot change the address where it points . In this case, your only chance is changing the data pointed by message.

该语句使编译器创建 4 个字符变量的内存空间。该内存簇的起始地址是 的指针值message。address ofmessage是不可更改的,您不能更改它指向的地址。在这种情况下,您唯一的机会是更改message.

##代码##

In this time, memory is created to store the address of pointer, so the address where messagepoint can change during execution. Then you can safely do message="bar"

这时候会创建内存来存放指针的地址,所以指针所在的地址message在执行过程中会发生变化。然后你可以安全地做message="bar"