Array type char[] is not assignable [duplicate]
Asked Answered
D

4

59

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.

#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);

}
Dipstick answered 31/8, 2015 at 14:3 Comment(3)
if you do want to assign to word without strcpy you can make word typed like const char *word.Encipher
Surprisingly, you get the error because array type char[] is not assignable. In C, you can't assign strings like you do, because there is no built-in string type, just raw character arrays. Anyway, this is such a FAQ... someone please find a duplicate.Immaterialism
The accepted answer in the "duplicate" is not an answer (meta.stackexchange.com/questions/225370/…), just a link to a horrible FAQ.Heptameter
E
73

You can't assign to an array, only copy to it.

Use strcpy instead, like

strcpy(word, "Jump");
Epner answered 31/8, 2015 at 14:5 Comment(3)
suggesting some 3rd party library function like strcpy without identifying where/what external library needs to be downloaded and installed to access it is the opposite of future-proof.Pouch
@DavidBandel strcpy is a standard C function. It's most definitely not "3rd party".Epner
add this line to use strcpy #include<string.h>Lithoid
D
21

TL;DR answer : An array name is not a modifiable lvalue. So, you cannot use the assignment operator (=) on that.

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


Now, to elaborate the actual reason behind the error message, quoting C11, chapter §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,

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

So, an array name is not a modifiable lvalue hence, you cannot assign anything to it. This is the reason behind the error message.

Dari answered 31/8, 2015 at 14:9 Comment(2)
But I wonder why the C spec is defined this way? Seems reasonable to want to assign from one array to another...Altercation
@Altercation I can think of two related reasons - 1) there's no operator overloading in C 2) array names, decay to the pointer to the first element in most of the cases (including when used as operand of the assignment operator) - so there's no direct way to copy the contents. At most, the address can be used - but that's not the use case we're looking for here.Dari
C
15

The = operator cannot be used to copy the contents of one array to the other; you must use a library function like strcpy or strcat for strings, memcpy for non-strings (or assign array elements individually).

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.

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

Instead, whenever the compiler sees an array expression in a statement, it converts that 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 sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration).

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

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 pointer value to an array object, which a) isn't a pointer, and b) cannot be assigned to anyway.

Chrismatory answered 31/8, 2015 at 15:5 Comment(0)
B
5

Use strcpy from <string.h>-

 strcpy(word,"Jump");

And similar for rest of them.

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

Bulldozer answered 31/8, 2015 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.