Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用结构的矩阵_C_Arrays_Matrix_Struct_Declaration - Fatal编程技术网

C 使用结构的矩阵

C 使用结构的矩阵,c,arrays,matrix,struct,declaration,C,Arrays,Matrix,Struct,Declaration,我有一个矩阵和一个struct,其中包含2个int变量 struct virus { int gen; int neighbours; } 我想用1值初始化我的完整gen矩阵。问题是它对矩阵的第一列不起作用。 我会把密码写下来。 另外,当我试图将我的矩阵设置为病毒结构时,它也不起作用,我必须初始化一个新的矩阵,我称之为b。 这只是一个简单的初始化和打印 #include <stdio.h> struct virus { int gen; int neigh

我有一个矩阵和一个
struct
,其中包含2个
int
变量

struct virus {
int gen;
int neighbours;
}
我想用
1
值初始化我的完整
gen
矩阵。问题是它对矩阵的第一列不起作用。 我会把密码写下来。 另外,当我试图将我的矩阵设置为
病毒结构时,它也不起作用,我必须初始化一个新的矩阵,我称之为
b
。 这只是一个简单的初始化和打印

    #include <stdio.h>

struct virus {
    int gen;
    int neighbours;
    };

void initGen(int n, int a[][100])
{
    struct virus b[n][100];

    int i,j;
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
            {
              b[i][j].gen = 1;
            }
    }
}

void printMatrixGen(int n, int b[][100])
{
    struct virus a[n][100];

    int i;
    for(i = 0; i < n; i++)
    {
        int j;
        for(j = 0; j < n; j++)
            printf("%d\t", a[i][j].gen);
        printf("\n");
    }
}

int main(void)
{
    int a[100][100], n;
    n = 4;
    initGen(n,a);
    printMatrixGen(n,a);
    return 0;
}
而不是

1    1    1   1
1    1    1   1
1    1    1   1
1    1    1   1

您的代码传递了错误的数组。您需要按如下方式更改函数签名:

void initGen(int n, struct virus a[][100]);
void printMatrixGen(int n, struct virus a[][100]);
然后,删除
struct virus b
数组的本地声明,并使用作为参数传递的
struct
s

最后,在
main
中声明
struct
数组,并将其传递给两个函数:

struct virus a[100][100];
int n = 4;
initGen(n, a);
printMatrixGen(n, a);

您的代码传递了错误的数组。您需要按如下方式更改函数签名:

void initGen(int n, struct virus a[][100]);
void printMatrixGen(int n, struct virus a[][100]);
然后,删除
struct virus b
数组的本地声明,并使用作为参数传递的
struct
s

最后,在
main
中声明
struct
数组,并将其传递给两个函数:

struct virus a[100][100];
int n = 4;
initGen(n, a);
printMatrixGen(n, a);

为什么要将数组作为参数传递,忽略它,然后初始化本地数组?这段代码没有任何意义,这就是你出错的原因。顺便说一句:它不起作用不是一个问题描述。你需要告诉我们实际发生了什么以及你期望发生什么。我刚刚编辑了代码并添加了程序给我的输出和它应该提供的输出。我必须将我的矩阵作为参数传递,因为我不能使用任何全局变量。在函数
printMatrixGen
initGen
中,你只能使用它们本地数组。为什么要将数组作为参数传递,忽略它,然后初始化本地数组?这段代码没有任何意义,这就是你出错的原因。顺便说一句:它不起作用不是一个问题描述。你需要告诉我们实际发生了什么以及你期望发生什么。我刚刚编辑了代码并添加了程序给我的输出和它应该提供的输出。我必须将我的矩阵作为参数传递,因为我不能使用任何全局变量。在函数
printMatrixGen
initGen
中,你只能使用它们本地阵列。非常有帮助!非常感谢。多亏了你,现在我明白了我应该如何使用结构。这个表达式:
struct virus a[100][100],n=4不正确,因为它将变量“n”声明为“struct virus”的实例。建议将该表达式移到类似以下内容的新行:
int n=4
@user3629249非常感谢!帮了大忙!非常感谢。多亏了你,现在我明白了我应该如何使用结构。这个表达式:
struct virus a[100][100],n=4不正确,因为它将变量“n”声明为“struct virus”的实例。建议将该表达式移到类似以下内容的新行:
int n=4
@user3629249非常感谢!