我一直遇到一個讀取結構數組值的問題,我一直在得到一個分段錯誤。下面是代碼:結構數組分段錯誤
int main()
{
/* Get PATH Environment Variable */
char *pathv[MAX_PATHS];
char cmd_line[MAX_ARGS];
struct command_t cmd[3]; //THIS IS THE STRUCT ARRAY
size_t len = 0;
/* Parse The Path */
ParsePath(pathv);
while(1) {
/* Print Continuous Prompt */
PrintPrompt();
/* Read Command Line & Parse It */
ReadCommand(cmd_line);
ParseCommand(cmd_line, cmd); //PASSING IT TO THIS METHOD
}
return 0;
}
int ParseCommand(char *buffer, struct command_t *cmd)
{
char *name = "Test";
cmd[0].name; //IF THIS IS COMMENTED OUT AND THE OTHER LINE AS WELL, PROGRAM RUNS
printf("%s\n", cmd[0].name); //FINE....THESE TWO LINES CAUSE SEG. FAULT
}
struct command_t {
char *name;
int argc;
char *argv[MAX_ARGS];
};
我在做什麼錯?非常感謝您的幫助。
你在哪裏分配給'CMD [0 ] .name'? – vroomfondel
您可能還想向我們展示'struct command_t'的定義。 – alk
對不起,我剛剛添加了我的結構代碼。這個結構在一個單獨的頭文件中! –