gpt4 book ai didi

C编程使用函数和calloc从文件中读取

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:38 25 4
gpt4 key购买 nike

我无法利用函数让程序的第二个 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) 函数中的函数时,程序崩溃了。 我认为我错误地调用了函数,混淆了指针的使用和适当变量的引用调用。

一些帮助将不胜感激。

谢谢

最佳答案

for (i = 0; i < * r; i ++ )

r 属于 int ** , 所以 i < *r比较 int ( i ) 和 int * (*r)。换句话说,您正在与地址进行比较,这不是您想要的。

关于C编程使用函数和calloc从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50309592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com