gpt4 book ai didi

C语言,改变char变量

转载 作者:行者123 更新时间:2023-12-04 08:24:47 24 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

9 个月前关闭。




Improve this question



int main()            
{
charactername="tom";
characterage=78;
printf("his name is %s\n", charactername);
printf("his age is %d\n", characterage);

characterage=78;
charactername="John";
printf("his name changed to %s\n", charactername);
printf("his age changed to %d\n", characterage);

return 0;
}

当我在代码之间更改字符名称时,代码不会运行,而如果我只更改年龄代码运行完美,为什么我不能更改字符变量但我可以更改字符年龄??(在 c 语言中)

最佳答案

两件事情:

  • 您必须定义变量的类型。
  • 无法分配数组。

  • 在您的代码中,您需要进行一些更改(在下面的注释中标记)
    #include <stdio.h>
    #include <string.h>
    // header files are needed
    #define ARRSIZ 64 // choose any size you like
    int main(void) // proper signature
    {
    char charactername[ARRSIZ]="tom"; //define a char array and initialize it.
    int characterage=78; // type is a must
    printf("his name is %s\n", charactername);
    printf("his age is %d\n", characterage);

    characterage=78; // this is fine, only assignment
    strcpy(charactername,"John"); // use strcpy to copy to an array
    printf("his name changed to %s\n", charactername);
    printf("his age changed to %d\n", characterage);

    return 0;
    }

    关于C语言,改变char变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65319274/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com