C语言全局结构体数据在main函数中读取异常求助
问题:无法正确访问全局结构体configInput的数据
我创建了包含如下结构体的头文件functions.h:
typedef struct ConfigData { char hostServerName[256]; unsigned short int portNmbr; char gameKind[256]; } config; extern config configInput;
在config.c文件中,我实现了readConfig函数为全局变量configInput赋值,并且能正确打印结构体数据:
#include "functions.h" config configInput; char* splitString (char *lineInFile){ char *word = strtok (lineInFile, " "); word = strtok (NULL, " "); word = strtok (NULL, " "); return word; } // Function removing spaces from a string char * removeSpacesFromStr(char *string) { int non_space_count = 0; for (int i = 0; string[i] != '\0'; i++) { if (string[i] != ' ') { string[non_space_count] = string[i]; non_space_count++;//non_space_count incremented } } string[non_space_count] = '\0'; return string; } void readConfig (char* configFile){ FILE *fPointer= fopen (configFile, "r"); if (fPointer == NULL){ perror("ERROR: Couldnt open confg file!\n"); } char bufferIn[256]; int count = 0; while(fgets(bufferIn, 256, fPointer)) { // eventuell != NULL if(strcmp(bufferIn, "") != 0){ bufferIn[strcspn(bufferIn, "\n")] = 0; switch (count) { case 0: strcpy(configInput.hostServerName, splitString(bufferIn)); break; case 1: configInput.portNmbr = atoi(splitString(bufferIn)); break; case 2: strcpy(configInput.gameKind, splitString(bufferIn)); break; } count++; } } printf("\n>>>Config File Data<<<\n"); printf("HostServerName: %s\n", configInput.hostServerName); printf("PortNumber: %d\n", configInput.portNmbr); printf("GameKind: %s\n\n ", configInput.gameKind); }
但在main函数中调用readConfig后,尝试读取configInput结构体数据时,只能得到随机字符,无法获取正确值:
#include "functions.h" int main (int argc, char *argv[]) { char gamekindname[256]= "NMMorris"; char *hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de"; int portnumber = 1357; char* gameID = argv[2]; char playerNumber[256]; char configFile[256] = "client.conf" ; ... //read in Data from config File readConfig(configFile); config configInput; strcpy(gamekindname, configInput.gameKind); strcpy(hostname, configInput.hostServerName); portnumber = configInput.portNmbr; }
问题原因
核心问题是你在main函数里重新定义了一个局部的configInput变量,覆盖了全局变量:
- 头文件中用
extern config configInput;声明了全局变量,config.c里定义了这个全局变量,readConfig操作的正是这个全局变量,所以函数内的打印正常。 - 但在
main函数调用readConfig后,你写了config configInput;,这会在main的局部作用域创建一个新的、未初始化的config类型变量,它和全局的configInput重名了。 - 后续你读取的是这个局部变量的值,而局部变量未初始化,所以得到的是随机垃圾值。
另外注意:main里的char *hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de";是错误写法——char *hostname[256]是指针数组,不能直接用字符串常量初始化,应该改成char hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de";,否则后续strcpy会引发错误。
解决方案
- 删除
main函数中的config configInput;这一行,直接使用全局的configInput变量。 - 修正
hostname的定义,改成字符数组而非指针数组。
修改后的main相关代码片段:
#include "functions.h" int main (int argc, char *argv[]) { char gamekindname[256]= "NMMorris"; char hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de"; // 修正此处 int portnumber = 1357; char* gameID = argv[2]; char playerNumber[256]; char configFile[256] = "client.conf" ; ... //read in Data from config File readConfig(configFile); // 去掉局部变量定义 strcpy(gamekindname, configInput.gameKind); strcpy(hostname, configInput.hostServerName); portnumber = configInput.portNmbr; }
内容的提问来源于stack exchange,提问作者Enno Mühlbauer
相关产品推荐
相关产品推荐

