在C语言中,我尝试使用一种结构来存储播放器的当前数据,并在结构中进行管理:
#include <stdio.h>
#include <stdlib.h>
typedef struct playerdat{
char playername[20];
int maxhp;
}playerdat;
void new_game(struct playerdat *player){
printf("Enter your character's name: \n");
scanf("%s", &player->playername);
player->playername = strcpy("Little", player->playername);
}
int main(){
struct playerdat player;
new_game(&player);
return 0;
}
一切工作正常,直到我尝试更改该结构的元素。
错误消息显示为:错误:从'char *'类型分配为'char [20]'类型时不兼容的类型
您能给我示范一个如何在这里使用指针的例子吗?
谢谢您的帮助!
最佳答案
scanf("%s", &player->playername);//don't need &
player->playername = strcpy("Little", player->playername);//you know,the memeber `playername` is a array,`playername` is the name of the array,so you can't modify the name of a array