- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为命令行 shell 实现历史记录功能。我已经实现了一个循环数组来保存十个最近的命令。每个命令还用一个整数来标记,指定总命令是什么。例如,如果总共输入 30 个命令,则循环数组中的 10 个命令将编号为 (30, 29, 28, 27,...,21)。
如果用户要插入命令“r”,后跟一个标记十条指令之一的数字,则该指令应该运行。当我试图确保正确接受两个字的命令时,我不断遇到段错误。谁能帮忙指出问题所在。
int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */
int position, count, rnum = 0;
char historyArray[10][MAX_LINE];
char *holder[MAX_LINE]={0};
while (1){ /* Program terminates normally inside setup */
background = 0;
printf("COMMAND->");
fflush(0);
setup(inputBuffer, args, &background); /* get next command */
position = (count % MOD_VAL);
strcpy(historyArray[position],args[0]);
if(!strcmp("rr",args[0]))
{
strcpy(historyArray[position],historyArray[((position-1)+MOD_VAL)%MOD_VAL]);
printf("%i",count);
printf("%c",'.');
printf("%c",' ');
printf("%s",historyArray[position]);
printf("%c",'\n');
strcpy(args[0],historyArray[position]);
}
else if(!strcmp("r",args[0])) //SEG FAULT OCCURING IN THIS ELSE-IF BLOCK!
{
//args[1] will hold given number
printf("%c",'\n');
printf("%s",args[0]);
printf("%s",args[1]);
printf("%s",args[2]);
printf("%c",'\n'); //PRINT STATEMENTS FOR DEBUGGING
strncpy(holder[0], args[2], MAX_LINE - 1); //SEG FAULT
rnum = atoi(args[1]);
strcpy(historyArray[position],historyArray[((position-(count-rnum))+MOD_VAL)%MOD_VAL]);
strcpy(args[0],historyArray[position]); //CHANGES VALUES OF args[1], args[2]
if(holder[0] != NULL)
{
strncpy(args[1],holder[0],MAX_LINE-1);
args[2] = NULL;
}
else
{
args[1] = NULL;
}
printf("%c",'\n');
printf("%s",args[0]);
printf("%s",args[1]);
printf("%s",args[2]);
printf("%c",'\n');
}
else if(!(strcmp("h",args[0]))||!(strcmp("history",args[0])))
{
int counter = 0;
while(counter < 10)
{
printf("%i",(count - counter));
printf("%c",'.');
printf("%c",' ');
printf("%s", historyArray[((position - counter + MOD_VAL)%MOD_VAL)]);
printf("%c",' ');
printf("%c",'\n');
counter ++;
if(counter > count)
break;
}
}
count++;
pid_t pid1; //Initialize pid_t variable to hold process identifier
pid1 = fork(); //Fork process and assign process identifier to "pid1"
if (pid1 == 0) //Child process
{
//Child process executes the command specified by the user and
//then quits.
execvp(args[0], args);
exit(0);
}
else //Parent process
{
if (background != 1)//Check for inclusion of '&' in command
{
wait(NULL); //Wait for child process to finish executing
}
}
/* the steps are:
(1) fork a child process using fork()
(2) the child process will invoke execvp()
(3) if background == 0, the parent will wait,
otherwise returns to the setup() function. */
}
}
感谢任何帮助!
-哑光
最佳答案
这里的 args 是字符指针数组。
但是strcpy
需要两个参数 - 这应该是数组
或由malloc分配的内存的字符指针
但是您的 strcpy(historyArray[position],args[0]);
将一个参数作为字符指针
,这将不会被接受。
因此您可以将 args[]
更改为 args[][]
或 args[0] = malloc(some_no)
, segfault
将被删除。
关于c - 段错误 - strcpy() - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286014/
请解释以下程序中发生了什么。 我在程序的开头和结尾检查了 strerror(errno) 返回的地址,并确认它每次都返回相同的地址。然后一旦确定这一点,在第一种情况下我继续将相同的地址分配给 ptr,
我正在尝试使用指针将字符串 str 复制到 str 中。我认为这可以使用单个指针本身来实现。每当我运行我的代码时,我都会显示 str1 但带有过多不需要的垃圾字符。请帮忙。提供一些见解 #includ
长话短说,我正在制作这款 3D OpenGL 构建器游戏。细节并不重要,但这是我的问题。我有这个字符串数组: char building_category_names_UPPER_CASE[][30]
我想使用 strcpy 将我所有的参数连接成一个字符指针。我正在这样做,但我的输出只是一个空格。 我使用了 3 个变量:'concat'(应该是我的输出)、'size' 来获取所有参数的长度和'kar
如果我有一个字符串数组 char* q[2]; q[0] = "Hello"; q[1] = "World"; 我想将 q[0] 放入一个新的字符串数组中 char* x[2]; 在 x[0] 中调用
我正在查看一个 strcpy 示例,其中他们增加了一个指针的值,并在 1 行中分配它,如下所示: *ptrA++ = *ptrB++; 我知道char数组中指针指向的值增加了,复制了内容。 c 会做类
我在 C 程序中使用了一个特定的数据结构,我用它来将类型信息附加到值。一个简单的示例如下所示: #include #include #include struct teststruct {
我需要将用户的输入存储到字符串数组中。 #include #include #include char *history[10] = {0}; int main (void) { cha
#include #include int main() { char *str, *temp; str = malloc(sizeof(char) * 100); fge
我有这样一个基本结构 typedef struct struck { char* id; char* mat; int value; char* place; } *T
我有一个以 开头的函数 void findErrors(int lineIndex){ char *line; strcpy(line, lines[lineIndex]); 然后调用另一个函
我运行了以下程序: #include #include #include int main() { char *p, *q; p = (char*)malloc(1); q =
Closed. This question is off-topic。它当前不接受答案。
每次我尝试将字符串插入程序中的二叉树时,我都必须使用 strcpy 并使用 strcpy 的目标才能成功插入。如果我不使用 strcpy 并仅使用源代码,程序将会崩溃。 示例: char *origi
我的代码给我带来了段错误。我调试了一下,执行strcpy时出现错误。该代码尝试从文本文件中提取数据并将其存储到结构数组中。我计划使用 strcpy 将文本文件的数据存储到结构中。知道为什么会发生这种情
#include #include #include char *vStrs[] = {"Max", "Moritz", "Bolte", "Hans Huckebein", "Helene",
我想知道是否有人可以帮助我解决这个段错误,valgrind 和 gdb 指向 strcpy(),但我仍然找不到它.. void listusers(userstat* ptustat) { F
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个关于 strcpy() 的问题。据我了解,我认为 strcpy 复制位于指针处的字节,而不是指针本身。但这段代码似乎 react 不同。 char* str2 = (char*) mallo
我正在开发一个使用 UDP 协议(protocol)传输文件的项目,但是当我使用 strcpy() 将缓冲区复制到另一个字符串时,它总是丢失一些字符。 简单的想法是:我定义了一个结构: struct
我是一名优秀的程序员,十分优秀!