Can';t解决故障错误
这并不是程序的全部,但它不会运行超过此函数。在fscanf语句附近似乎有segfault,但我不认为这是导致错误的原因。我真的没有看到任何可能导致segfault错误的东西,但我是个业余爱好者,可能错过了什么。所有printf语句都用于调试目的Can';t解决故障错误,c,segmentation-fault,C,Segmentation Fault,这并不是程序的全部,但它不会运行超过此函数。在fscanf语句附近似乎有segfault,但我不认为这是导致错误的原因。我真的没有看到任何可能导致segfault错误的东西,但我是个业余爱好者,可能错过了什么。所有printf语句都用于调试目的 #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #define N 10 struct poi
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define N 10
struct point2d_def
{
int x;
int y;
};
typedef struct point2d_def point2d;
void fill(char str[], point2d P[])
{
FILE *ifp = NULL;
ifp = fopen(str, "r");
int i = 0;
if (ifp == NULL)
{
printf("Could not open %s for reading\n", ifp);
exit(1);
}
for(i = 0; i < N; ++i)
{
fscanf(ifp, "%d %d", &P[i].x, &P[i].y);
}
fclose(ifp);
printf("Fill function passed\n");
return;
printf("Blorp");
}
#包括
#包括
#包括
#包括
#定义n10
结构点2D_def
{
int x;
int-y;
};
typedef struct point2d_def point2d;
空洞填充(char str[],point2d P[]
{
文件*ifp=NULL;
ifp=fopen(str,“r”);
int i=0;
如果(ifp==NULL)
{
printf(“无法打开%s进行读取,\n”,ifp);
出口(1);
}
对于(i=0;i
首先,在func中声明p[N]
,并在参数中接收p
第二,您正在写入11个插槽,而不是10个插槽:
for(i = 0; i < N; ++i){
fscanf(ifp, "%d %d", &P[i].x, &P[i].y);
}
(i=0;i{
fscanf(ifp,“%d%d”,&P[i].x,&P[i].y);
}
删除
=
..数组p如何声明?参数不能是数组,请使用指针,而不是p
如何分配?可能您为它分配了N
(10)个条目,但您正在使用fscanf
写入循环中的11个位置。如何调用fill
?请发布一个。尝试使用调试器查看错误发生在哪一行。我已更改所有内容以匹配您的答案,但仍然存在分段错误;还有什么我可能弄糟或没有包含在代码中的吗?请显示您声明的部分P
,以及您称之为fill
func的行