【发布时间】:2011-09-29 04:26:54
【问题描述】:
所以当我在以下情况下调用 printf 时会出现段错误。我只是看不出我做错了什么。有任何想法吗?太感谢了。我已经在代码中用注释标记了我得到 seg 错误的位置(在第一块代码中)。
...
char* command_to_run;
if(is_command_built_in(exec_args, command_to_run)){
//run built in command
printf("command_to_run = %s\n", command_to_run); // <--- this is where the problem is
run_built_in_command(exec_args);
}
...
int is_command_built_in(char** args, char* matched_command){
char* built_in_commands[] = {"something", "quit", "hey"};
int size_of_commands_arr = 3;
int i;
//char* command_to_execute;
for(i = 0; i < size_of_commands_arr; i++){
int same = strcmp(args[0], built_in_commands[i]);
if(same == 0){
//they were the same
matched_command = built_in_commands[i];
return 1;
}
}
return 0;
}
【问题讨论】:
标签: c pointers segmentation-fault printf