我无法利用函数来获取程序的第二个calloc来正确读取文件。要调用的函数是colAlloc (variables)
。
我的代码如下:
void readSpaces (FILE * ptrF, char fileN [], double ** m, int * r)
{
ptrF = fopen (fileN, "r");
char s;
int i;
int ctrS = 0;
int c;
int cVal;
for (s = getc (ptrF); s != EOF; s = getc (ptrF))
{
if (s == ' ' || s == '\t' || s == '\t')
{
++ctrS;
}
}
cVal = ctrS / * r;
c = cVal;
colAlloc (ptrF, fileN, m, &r, &cVal);
/**something is not working here so the program is giving a run-time error once it needs to read column**/
fclose (ptrF);
}
//allocate memory for column
void colAlloc (FILE * ptrF, char fileN [], double ** m, int ** r, int ** s) //file pointer, file name, matrix, row, spaces;
{
int i;
int c;
c = & s;
for (i = 0; i < * r; i ++ )
{
m [i] = (double *) calloc (c, sizeof (double));
if (m [i] == NULL)
{
printf ("\nSorry, not enough memory!\n\n");
exit (0);
}
}
printf ("Cols >> %d.\n\n", c);
for (i = 0; i < * r; i ++)
{
free (m [i]);
}
}
当我在readSpaces (ptrF, fileN, m, r)
函数中调用该函数时,程序崩溃了。 我认为我正在调用函数错误,并且混淆了指针的使用和通过引用调用适当的变量。
非常感谢一些帮助。
由于
答案 0 :(得分:3)
for (i = 0; i < * r; i ++ )
r属于int **
,因此i < *r
将int(i
)与int *
(*r
)进行比较。换句话说,你正在与一个地址进行比较,这不是你想要的。