[英]How to fix segmentation fault: 11 compiler error
I have been learning from the C Programming Language book (K&R) and was writing one of the exercises that removes trailing blanks from an input. 我一直在学习《 C编程语言》(K&R)书,并且正在编写其中一项从输入中删除尾随空白的练习。 I understand that a segmentation fault is at some level a problem having to do with accessing memory that is not accessible, but I have read through this code several times and can't find the error.
我知道分段错误在某种程度上是与访问不可访问的内存有关的问题,但是我已多次阅读此代码,并且找不到错误。 I would like it very much if someone could help find this error and tell me how to discover errors like this in the future.
如果有人可以帮助您找到此错误并告诉我以后如何发现此类错误,我非常希望它。
#include <stdio.h>
#define MAXLINE 1000
#define CHAR 0 /*character definition*/
#define TRAIL 1 /*determines whether program is in a trailing blank*/
int getinput(char input[], int max);
int trailrem(char input[], char copyto[]);
int len;
int main() {
char line[MAXLINE]; /*current line*/
char newline[MAXLINE];
int i, c, newreturn; /*integer counter, character holder, current line length, and trailrem return value*/
int len;
while((len = getinput(line, MAXLINE)) > 0) {
newreturn = trailrem(line, newline);
for(i = 0; i <= newreturn; ++i)
printf("\n%c\n", newline[i]);
}
}
int getinput(char input[],int max) {
int i, c, line;
for(i = 0; (c = getchar()) != EOF && c != '\n' && c < (max-1); ++i)
input[i] = c;
if(c == '\n') {
input[i] = c;
++i;
}
input[i] = '\0';
return i;
}
int trailrem(char input[], char copy[]) {
int i, j, minusin, state, r;
for(i = len; input[i] != EOF && i >= 0; --i) {
if(input[i] =='\n')
state = TRAIL;
else if((input[i] == ' ' && state == TRAIL) ||( input[i] == '\t' && state == TRAIL))
++minusin;
else if(state == TRAIL && (input[i] != ' ' || input[i] != '\t'))
state = CHAR;
for(j = (r = len-minusin); state == CHAR; --j){
copy[j-2] = input[i];
}
}
copy[r] = '\0';
copy[r-1] = '\n';
return r;
}
So many problems in your code. 您的代码中有很多问题。 But the main problem is, you have a
global
len
但主要的问题是,你有一个
global
len
int len;
And a local len
in the main
function. 并具有本地
len
的main
功能。
You are initializing len
in main
function like this: 您正在像这样在
main
功能中初始化len
:
while((len = getinput(line, MAXLINE)) > 0)
So the local len
is updated. 因此,本地
len
被更新。 But the global len
is still 0
. 但是全局
len
仍然为0
。
You are expecting that, you will get the updated value of len
in trailrem
method but you don't. 您期望这样,您将在
trailrem
方法中获得len
的更新值,但没有。 In trailrem()
you will get len
equal to 0
! 在
trailrem()
您将得到len
等于0
!
for(i = len; input[i] != EOF && i >= 0; --i)
So i
is 0
too. 所以
i
也是0
。 And hence, copy[r-1] = '\\n';
因此,
copy[r-1] = '\\n';
will crash, because r-1
can be negative. 将崩溃,因为
r-1
可以为负。
Other problems: (BLUEPIXY and WhozCraig mentioned in the comment). 其他问题:(评论中提到了BLUEPIXY和WhozCraig)。
for(i = 0; (c = getchar()) != EOF && c != '\n' && c < (max-1); ++i)
here, c < (max-1)
should be i < (max-1)
. 在这里,
c < (max-1)
应该是i < (max-1)
。
++minusin;
in trailrem
function where minusin
is uninitialized. 在
trailrem
函数中, minusin
未初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.