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

C programming reading from file using functions & calloc

我无法利用函数使程序的第二个 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) 进行比较。换句话说,您正在与地址进行比较,这不是您想要的。