Array type char[] is not assignable

SkytheCoder picture SkytheCoder · Aug 31, 2015 · Viewed 78k times · Source

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

}

Answer

Some programmer dude picture Some programmer dude · Aug 31, 2015

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

Use strcpy instead, like

strcpy(word, "Jump");