C语言 数组类型 char[] 不可分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32313150/
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 12:05:17  来源:igfitidea点击:

Array type char[] is not assignable

carraysstringassignment-operator

提问by SkytheCoder

Well here is my first post. I've been trying to do this choice choosing thing and I want the user to choose only numbers instead of typing them down (easier) but when I want the numbers to equal a string, it says "array type char[30]is not assignable". Even if at the back I put semi-colon or not.

嗯,这是我的第一篇文章。我一直在尝试做这个选择选择的事情,我希望用户只选择数字而不是输入它们(更容易)但是当我希望数字等于一个字符串时,它会说“数组类型char[30]不可分配”。即使在后面我是否放了分号。

#include <stdio.h>

int main() {
  int choice1;
  char word[30];

  printf("You have three choice.\n");
  printf("[1] Jump [2] Run [3] Dance\n");
  scanf("%d",&choice1);
  if (choice1 == 1)
  {
    word = "Jump" //Error #1
  }
  else if (choice1 == 2)
  {
    word = "Eat" //Error #2
  }
  else if (choice1 == 3)
  {
    word = "Sleep"; //Error #3
  }

  printf("You will now be %sing",word);

}

回答by Some programmer dude

You can't assignto an array, only copyto it.

您不能分配给数组,只能复制到它。

Use strcpyinstead, like

使用strcpy,而不是像

strcpy(word, "Jump");

回答by Sourav Ghosh

TL;DRanswer : An array nameis nota modifiable lvalue. So, you cannot use the assignment operator (=) on that.

TL; DR答案:数组名称修改的左值。因此,您不能在其上使用赋值运算符 ( =)。

To copy the content into the array, you need to use strcpy()from string.h(chararray) or memcpy()in general.

要将内容复制到数组中,您需要使用strcpy()from string.h( chararray) 或memcpy()一般情况下。



Now, to elaborate the actual reasonbehind the error message, quoting C11, chapter §6.5.16, Assignment operators

现在,详细说明错误消息背后的实际原因,引用C11第 6.5.16 章,Assignment operators

assignment operator shall have a modifiable lvalue as its left operand.

赋值运算符应有一个可修改的左值作为其左操作数。

and then, quoting chapter §6.3.2.1 from the same standard,

然后,从同一标准中引用第 6.3.2.1 章,

A modifiable lvalueis an lvalue that does not have array type, [....]

修改的左值是不具有左值数组类型,[...]

So, an array name is not a modifiable lvaluehence, you cannot assignanything to it. This is the reason behind the error message.

因此,数组名称不是可修改的左值,因此您不能为其分配任何内容。这就是错误消息背后的原因。

回答by John Bode

The =operator cannot be used to copy the contentsof one array to the other; you must use a library function like strcpyor strcatfor strings, memcpyfor non-strings (or assign array elements individually).

=运算符不能用于将一个数组的内容复制到另一个数组;对于非字符串(或单独分配数组元素),您必须使用类似strcpystrcat用于字符串的库函数memcpy

This is a consequence of how C treats array expressions. An array expression is defined by the language standard to be a non-modifiable lvalue; it's an lvalue because it refers to an object in memory, but it may not be the target of an assignment.

这是 C 处理数组表达式的方式的结果。语言标准将数组表达式定义为不可修改的左值;它是一个左值,因为它指的是内存中的一个对象,但它可能不是赋值的目标。

The array subscript operation a[i]is defined as *(a + i); that is, given the array addressa, offset ielements from that address and dereference the result. Since the array expression ais treated as a pointer, most people think avariable storesa pointer to the first element of the array, but it doesn't. All that gets stored are the array elements themselves.

数组下标操作a[i]定义为*(a + i);也就是说,给定数组addressai从该地址偏移元素并取消引用结果。由于数组表达式a被视为一个指针,大多数人认为a变量存储了一个指向数组第一个元素的指针,但事实并非如此。所有存储的都是数组元素本身。

Instead, whenever the compiler sees an array expression in a statement, it convertsthat expression from type "N-element array of T" to "pointer to T", and the value of the expression becomes the address of the first element of the array (unless the expression is the operand of the sizeofor unary &operators, or is a string literal being used to initialize another array in a declaration).

相反,每当编译器在语句中看到数组表达式时,它就会将该表达式从“N 元素数组”类型转换T为“指向”的指针T,并且表达式的值成为数组第一个元素的地址(除非表达式是sizeof或 一元运算&符的操作数,或者是用于在声明中初始化另一个数组的字符串文字)。

And this is why an array expression like wordcannot be the target of an assignment; there's nothing to assign to. There's no object wordthat exists independently of word[0], word[1], etc.

这就是为什么像这样的数组表达式word不能作为赋值的目标;没有什么可分配。有没有对象word是独立的存在word[0]word[1]等等。

When you write

当你写

word = "Jump";

the type of the expression "Jump"is converted from "5-element array of char" to "pointer to char", and the value of the expression is the address of the first element of the array. And you're trying to assign that pointervalue to an arrayobject, which a) isn't a pointer, and b) cannot be assigned to anyway.

表达式的类型"Jump"从“五元素数组char”转换为“指向”的指针char,表达式的值为数组第一个元素的地址。并且您试图将该指针值分配给一个数组对象,该对象 a) 不是指针,并且 b) 无论如何都不能分配给它。

回答by ameyCU

Use strcpyfrom <string.h>-

使用strcpy<string.h>-

 strcpy(word,"Jump");

And similar for rest of them.

其余的也类似。

You just can't do word ="Jump". As the contents are modifiable, the arraysthemselves are not.

你就是做不到word ="Jump"。由于内容是可修改的,数组本身不可修改。