Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改c中的字符数组值?_C_Arrays_String_Char - Fatal编程技术网

如何更改c中的字符数组值?

如何更改c中的字符数组值?,c,arrays,string,char,C,Arrays,String,Char,我想更改字符数据的值 /*更改默认值时出错,是否有其他方法*/ char name[]="Ronaldo"; int choice='0'; printf("Select Player"); printf("1.Messi\n 2.Suarez\n 3.Neymer\n 4.Dembele\n"); switch(choice) case 1: printf("Messi is Selected"); **Err

我想更改字符数据的值 /*更改默认值时出错,是否有其他方法*/

char name[]="Ronaldo";
int choice='0';

    printf("Select Player");
    printf("1.Messi\n 2.Suarez\n 3.Neymer\n 4.Dembele\n");
    switch(choice)
    case 1:
            printf("Messi is Selected");

            **Error Position**

            name="Messi";
            printf("Player changd to %s",name);
            break;

    case 5:
            printf("Exit");
            name="Muller";
            exit(1)
更改球员姓名

1。 您不能为
char
数组分配字符串,除非在初始化时使用以下方法完成:

char name[]="Ronaldo";
而是使用strcpy()(Header
string.h
)->
strcpy(名称,“mesi”)取而代之

考虑数组
名称
需要能够容纳新分配的字符串+空终止符,在本例中提供空终止符是因为字符串
“罗纳尔多”
的字符数比以下任何字符都多。如果情况并非如此,则必须使用适当数量的字符定义
名称
,或选择比任何其他名称都大的名称,否则程序将导致内存中的数据溢出

2. 请注意,如果
choice
始终为
0
,则
switch
语句没有意义,因为您从未读取
choice
的输入

另外,
开关的
案例
未包含在
{
}
中,这会导致编译错误

3.
选项
由字符初始化。虽然这是允许的,但在这种情况下没有意义,并且会混淆代码的读者。而是使用
int-choice=0


更正代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
   char name[]="Ronaldo";
    int choice=0;

    printf("Select Player\n");
    printf("1.Messi\n2.Suarez\n3.Neymar\n4.Dembele\n5.Muller\n\n");

    scanf("%d",&choice);

    switch(choice)
    {

    case 1:
            printf("\nMessi is Selected");
            strcpy(name,"Messi");

            break;


    case 2:
            printf("\nSuarez is Selected");
            strcpy(name,"Suarez");

            break;


    case 3:
            printf("\nNeymar is Selected");
            strcpy(name,"Neymar");

            break;    

    case 4:
            printf("\nDembele is Selected");
            strcpy(name,"Dembele");

            break;    


    case 5:
            printf("\nMuller is Selected");
            strcpy(name,"Muller"); 
           
            break;               

    default:
            printf("Exit");
            exit(1);
    } 

    printf("\nPlayer changed to %s",name);   
    return 0;
}
问题是

char name[]="Ronaldo";
给你一个字符数组这不是您想要的,因为字符数组不允许简单赋值。对于字符数组,您需要使用strcpy

但是-您需要的是指向char的指针--例如:

char* name="Ronaldo";
然后你可以做一些简单的作业,比如:

char* name="Ronaldo";
printf("My player is %s\n", name);
name = "Eriksen";
printf("My player is %s\n", name);
char指针可以直接指定为指向任何字符串文字

除此之外,您从未获得任何用户输入

请注意,
switch
语句有一个
default
-类似于:

switch(choice)
{
    case 1:
        // do some things for input 1
    break;

    case 2:
        // do some things for input 2
    break;

    default:
        // do some things for all other values
    break;
}
您缺少
main()
和许多其他内容。。。它可以像这样工作

#include <stdio.h>
#include <string.h>


int main(void){

char name[20] = "Ronaldo";
int choice;

    printf("Select Player:\n\n");
    printf("\t1.Messi\n\t2.Suarez\n\t3.Neymer\n\t4.Dembele\n\n");

    scanf("%d", &choice);

    switch(choice){
    case 1:
            strcpy(name, "Messi");
            printf("\t%s is selected...\n", name);
            break;

    case 2:
            strcpy(name, "Suarez");
            printf("\t%s is selected...\n", name);
            break;

    case 3:
            strcpy(name, "Neymer");
            printf("\t%s is selected...\n", name);
            break;

    case 4:
            strcpy(name, "Dembele");
            printf("\t%s is selected...\n", name);
            break;

    default:
            puts("\tWrong selection");
    }

return(0);

}
#包括
#包括
内部主(空){
char name[20]=“罗纳尔多”;
智力选择;
printf(“选择播放器:\n\n”);
printf(“\t1.mesi\n\t2.Suarez\n\t3.Neymer\n\t4.Dembele\n\n”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:
strcpy(名称“梅西”);
printf(“\t%s被选中…\n”,名称);
打破
案例2:
strcpy(名称“苏亚雷斯”);
printf(“\t%s被选中…\n”,名称);
打破
案例3:
strcpy(名称,“Neymer”);
printf(“\t%s被选中…\n”,名称);
打破
案例4:
strcpy(名称“Dembele”);
printf(“\t%s被选中…\n”,名称);
打破
违约:
放置(“\t错误选择”);
}
返回(0);
}

您的代码中有几个错误:

>char name[]=“罗纳尔多”;
这将创建具有预定义长度的字符数组。

>int-choice='0';
整数类型无法存储“0”


首先更正它们,然后提供有关您要查找的内容的详细信息。

错误:使用数组类型指定表达式您正在将字符指定给整数<代码>整数选择=0'这是第一个错误。。。而是写
int-choice=0也可以考虑使用<代码>(或)>代码>而不是<代码> Prtff()/>代码>如果打印的文本总是相同的。@ 71Ga不一定是错误的。至少是合法的C代码。首先,
charname[]=“罗纳尔多”
等于
字符名[8];strcpy(姓名,“罗纳尔多”)-编译器自动确定长度-so 1。事实并非如此。第二,实际上允许给
int
分配一个字符,所以“整数类型不能存储'0'”是错误的。有趣的是,我删除了我的注释。因此,为了不在内存中的任何位置获取悬空字符串文字,唯一的方法可能是分别分配每个字符和终止的空字符。@RobertSsupportsMonicaCellio更正,但这可能会使用比“悬空”字符串文字更多的内存。我还没有测试过,这只是我的猜测。
switch(choice)
{
    case 1:
        // do some things for input 1
    break;

    case 2:
        // do some things for input 2
    break;

    default:
        // do some things for all other values
    break;
}
#include <stdio.h>
#include <string.h>


int main(void){

char name[20] = "Ronaldo";
int choice;

    printf("Select Player:\n\n");
    printf("\t1.Messi\n\t2.Suarez\n\t3.Neymer\n\t4.Dembele\n\n");

    scanf("%d", &choice);

    switch(choice){
    case 1:
            strcpy(name, "Messi");
            printf("\t%s is selected...\n", name);
            break;

    case 2:
            strcpy(name, "Suarez");
            printf("\t%s is selected...\n", name);
            break;

    case 3:
            strcpy(name, "Neymer");
            printf("\t%s is selected...\n", name);
            break;

    case 4:
            strcpy(name, "Dembele");
            printf("\t%s is selected...\n", name);
            break;

    default:
            puts("\tWrong selection");
    }

return(0);

}