我是C的新手,想打破这一系列视频来尝试一个项目,但我遇到了分割错误。我明白这意味着我正在尝试访问我不被允许访问的内存,但我不认为有什么问题。。。我有很多代码,所以我会粘贴那些看起来可能导致问题的部分,并去掉包含部分。我正在制作一款命令行文本冒险游戏,两名玩家可以选择他们的名字、职业和种族(目前游戏在要求他们输入后结束(。我很感激:
main.c
int main() {
// See term dimesions
// Create Player
struct User* Player;
CreatePlayer(Player);
// Create Player
struct User* Player2;
CreatePlayer(Player2);
return 0;
}
story.c
void CreatePlayer(struct User* Player) {
char name[MAXINPUT];
char class[MAXINPUT];
char race[MAXINPUT];
// Ask for name
printf("%sWhat is your name?n:t", KNRM);
getInput(name);
printf("%sHello %s!n", KNRM, name);
{
// Ask for class
char question[] = "What is your class (Type '1' for Fighter or '2' for Mage)?n:t";
char options[] = "12";
char coorilations[][MAXINPUT] = {"Fighter", "Mage"};
questionLoop(question, options, coorilations, class);
InitializeClass(Player, class);
printf("%sYou are now a %s!n", KNRM, class);
}
{
// Ask for race
char question[] = "What is your race (Type '1' for Human or '2' for Elf)?n:t";
char options[] = "12";
char coorilations[][MAXINPUT] = {"Human", "Elf"};
questionLoop(question, options, coorilations, race);
printf("%sYou are now a %s!n", KNRM, race);
}
strcpy(Player->name, name);
strcpy(Player->race, race);
}
模块.c
void InitializeClass(struct User* Player, char* class) {
if (compare(class, "Fighter")) {
strcpy(Player->class.name, "Fighter");
Player->class.maxHealth = 10;
Player->class.health = 10;
Player->class.strength = 7;
} else if (compare(class, "Mage")) {
strcpy(Player->class.name, "Mage");
Player->class.maxHealth = 7;
Player->class.health = 7;
Player->class.strength = 10;
}
}
void getInput(char res[]) {
int i = 0;
int c;
printf("%s", KMAG);
c = getchar();
while(c != 'n') {
res[i++] = c;
c = getchar();
}
res[i] = ' ';
}
void questionLoop(char question[], char options[], char coorilations[][MAXINPUT], char var[]) {
printf("%s-------------------------------n", KGRN);
printf("%s%s", KNRM, question);
getInput(var);
for(int i = 0; i < strlen(options); i++) {
if (options[i] == var[0] && var[1] == ' ') {
strcpy(var, coorilations[i]);
printf("%s", KNRM);
return;
}
}
printf("%sSorry that was not an option...n", KNRM);
questionLoop(question, options, coorilations, var);
}
int compare(char one[], char two[]) {
for (int i = 0; one[i] != ' '; i++) {
if (one[i] != two[i]) {
return 0;
}
}
return 1;
}
模块.h
#ifndef HEADER_FILE
#define HEADER_FILE
#define MAXINPUT 20
#define KNRM "x1B[0m"
#define KRED "x1B[31m"
#define KGRN "x1B[32m"
#define KYEL "x1B[33m"
#define KBLU "x1B[34m"
#define KMAG "x1B[35m"
#define KCYN "x1B[36m"
#define KWHT "x1B[37m"
// Classes
struct Class {
char name[MAXINPUT];
int strength;
int health;
int maxHealth;
};
// User struct
struct User {
char name[MAXINPUT];
char race[MAXINPUT];
struct Class class;
};
// Utilities
void getInput(char res[]);
void questionLoop(char question[], char options[], char coorilations[][MAXINPUT], char var[]);
int compare(char one[], char two[]);
// Game Setup Functions
void CreatePlayer(struct User*);
void InitializeClass(struct User* Player, char* class);
// Game Functions
void GameLoop(struct User*);
void ViewStats(struct User*);
#endif
您从不为Player
分配内存,因此从您的主方法中,您调用CreatePlayer()
,它依次调用questionLoop()
,它在内存分配方面不做任何事情,然后调用InitializeClass()
,它做(以及其他(:
strcpy(Player->class.name, "Fighter");
但Player
并不存在,因为您实际上还没有创建它,也没有为它进行分配,因此会出现Segmentation错误(在该方法的下一行(。
尝试使用malloc()
(例如struct User* player = malloc(sizeof(struct User));
(动态分配内存,完成后不要忘记free()
这声明了一个指针:
struct User* Player;
然而,它完全没有初始化。在用某种东西填充它之前,不能使用它。CreatePlayer
职能部门应承担这一责任。
通常,这些功能看起来像:
struct User* CreatePlayer() {
struct User *player = malloc(sizeof(struct User));
if (player == NULL) {
return NULL;
}
// Do stuff with player
return player;
}
在该模式可用于任何";创建";类型函数。