参差不齐的数组和 For 循环错误
我在 for 循环中间遇到错误访问错误,总是在 i=4 时发生。有人知道这是为什么吗?它一直有效到 i=4,但我不明白为什么在 for 循环的任何其他部分不会出现错误的访问错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
I get a bad access error in the middle of the for loop, always when i=4. Does anybody know the reason for this? It works until i=4, but I don't see why I wouldn't get the bad access error in any other part of the for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,这个:
应该是:
而这个:
根本不应该在那里。
It looks to me as if this:
should be:
And this:
should not be there at all.