数组类型 char[] 不可分配 [英] Array type char[] is not assignable

查看:42
本文介绍了数组类型 char[] 不可分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

推荐答案

你不能赋值给一个数组,只能复制给它.

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

使用 strcpy 代替,例如

Use strcpy instead, like

strcpy(word, "Jump");

这篇关于数组类型 char[] 不可分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆