【发布时间】:2021-06-14 12:07:17
【问题描述】:
我正在编写一个用于执行终端“历史 10”命令的 c 代码,我在我的 mac 终端上使用 clang 编译器运行程序,它显示错误“非法指令:4” 我的代码是-
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include <unistd.h>
#include<string.h>
int main()
{ char cmd[10];
strcpy(cmd,"history 10");
system(cmd);
return 0;
}
【问题讨论】:
-
您需要
char cmd[11];否则不会存储NUL 字符('\0')。 -
Off-by-one 错误。 (不要吝啬缓冲区大小......)
-
让编译器完成工作:
char cmd[] = "history 10"; -
您确定需要包含所有这些
*.h文件吗?