在C

时间:2018-05-23 11:39:53

标签: c

使用函数中结构的参数时出现问题。我是C的新手,我在网上找不到任何相关的解决方案。请找到下面的代码,并帮助我如何使用函数中结构的参数。

typedef struct{
int rows;
int cols;
}matrix;

/* Find the number of rows and columns */
void computeRowsCols(matrix *str, int val)
{
    if (condition)
        {
        str.rows = 3;
        str.cols = 3;
        }
    else
        {
        str.rows = 6;
        str.cols = 6;
        }
}


/* Find the matrix with the number of rows and columns
void computeMatrix(int val)
{

    matrix str;
    computeRowsCols(&str,val);

    int A[str.rows][str.cols];
    int (*mat)[str.cols] = &A[str.cols]; /* Pointer to a matrix */

    if (condition)
        mat = func1(A);
    else
        mat = func2(A);

}

当我拨打 func1(A)时,我手动输入行数,列数。但是我需要从 computeRowsCols()函数中获取行,列的值,如下面 required 部分中所述

int (*func1( int A[3][3]))[3] 
/* required : int (*func1( int A[str.rows][str.cols]))[str.cols] */
{
...
return A;
}

int (*func2( int A[6][6]))[6]
/* required : int (*func2( int A[str.rows][str.cols]))[str.cols] */
{
...
return A;
}

int main()
{
    int val = 10;
    computeMatrix(val);
    return 0;
}

0 个答案:

没有答案