【发布时间】:2016-12-30 18:39:00
【问题描述】:
这是我的尾部代码(前 10 行):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef char storage_datatype;
#define MAXLINESIZE 1000
#define STORAGESIZE 10000
#define MAXLINES 100
int mgetline(char*, int);
char* alloc(int n);
void cp(char*, char*);
char *lines[MAXLINES];
storage_datatype storage[STORAGESIZE];
storage_datatype *storagep = storage;
int main(int argc, char **argv) {
int space, i;
space = i = 0;
char line[MAXLINESIZE];
char* p;
while ((space = mgetline(line, MAXLINESIZE)) > 0) {
p = alloc(space);
cp(p, line);
lines[i++] = p;
}
i = 0;
while (i < 10) {
if (*lines[i]) {
printf("%s", lines[i++]);
}
}
getchar();
return 0;
}
int mgetline(char *s, int lim)
{
int c;
char *t = s;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
*s++ = c;
if (c == '\n')
*s++ = c;
*s = '\0';
return s - t;
}
char* alloc(int n) {
if (storage + STORAGESIZE - storagep >= n) {
storagep += n;
return storagep - n;
}
else
return 0;
}
void cp(char *s, char *t) {
while ((*s++ = *t++));
}
我收到此错误:
访问冲突读取位置0x0000000000000000。
在这一行:
if (*lines[i]) {
我无法理解为什么。我希望有人能给我解释一下。
【问题讨论】:
-
space = mgetline(line, MAXLINESIZE) > 0-->(space = mgetline(line, MAXLINESIZE)) > 0 -
if (*lines[i]) {-->if (lines[i]) { -
@BLUEPIXY 第一条评论是有道理的。但是第二个没有意义?
-
@Stargateur 它在里面。
-
抱歉,如果您将
i++放入您的if放入您的while,我重复一遍。只有if中的条件为真,i的值才会增加。因此,如果条件为假,您将永远不会增加i,因此您将有一个无限循环。
标签: c access-violation tail