简体   繁体   English

在 c 中的结构中初始化多维数组

[英]initializing a multidimensional array in a struct in c

hi i'm working on this program that calculates the various calculations of a matrix (eg determinant and trace and stuff) and i want to use an array in a struct to create a matrix.嗨,我正在研究这个计算矩阵的各种计算(例如行列式和迹线和东西)的程序,我想在结构中使用数组来创建矩阵。

in my calculations.h header file, i have this:在我的calculation.h 头文件中,我有这个:

struct matrices {
     int matrix[3][3]; };

in my calculations.c implementation file i have this function that basically creates the matrix after taking integer inputs from the user:在我的calculation.c 实现文件中,我有这个函数,它在从用户那里获取整数输入后基本上创建矩阵:

struct matrices creation (int x, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8 ) {
     struct matrices mmm = {{x, x1, x2}, {x3, x4, x5}, {x6, x7, x8}};
     return mmm;
}

however, i get the error message error: extra brace group at end of initializer directed to the line struct matrices mmm = {{x, x1, x2}, {x3, x4, x5}, {x6, x7, x8}};但是,我收到错误消息error: extra brace group at end of initializer指向行struct matrices mmm = {{x, x1, x2}, {x3, x4, x5}, {x6, x7, x8}};

thanks :)谢谢 :)

in my calculations.c implementation file i have this function that basically creates the matrix after taking integer inputs from the user:在我的calculation.c 实现文件中,我有这个函数,它在从用户那里获取整数输入后基本上创建矩阵:

In general it is not practical to have a separate int for each position of the matrix.一般来说,为矩阵的每个位置都有一个单独的int是不切实际的。 What if it were a 30x30 matrix?如果它是一个 30x30 的矩阵呢?

Instead you use indexes to get each value from the user into the desired position.相反,您使用索引将每个值从用户获取到所需的位置。

Your function:你的功能:

struct matrices creation (
    int  x, int x1, int x2,
    int x3, int x4, int x5,
    int x6, int x7, int x8 ) {
     struct matrices mmm = {{x, x1, x2}, {x3, x4, x5}, {x6, x7, x8}};
     return mmm;
}

just takes 8 int to stuff into an array and them return a copy of the array, but is the same as只需要 8 个int填充到一个数组中,然后它们返回一个数组的副本,但与

  matrix another = { {x1, x2, x3}, {x4, x5, x6}, {x7, x8, x9} };

example例子

#include<stdio.h>
  
typedef int matrix[3][3];
int show(matrix);

int main(void)
{
  matrix one = {{1,2,3}, { 4,5,6}, { 7,8,9}};
  show(one);
  int x1,x2,x3,x4,x5,x6,x7,x8,x9;
  x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = 42;
  x5 = -4242;
  matrix another = { {x1, x2, x3}, {x4, x5, x6}, {x7, x8, x9} };
  show(another);
  return 0;
}

int show(matrix M)
{
    for ( int y=0;y<3;y+=1)
    {
        for( int x = 0;x<3;x+=1) printf("%6d ", M[y][x]);
        printf("\n");
    }
    printf("\n");
    return 0;
};

shows显示

     1      2      3 
     4      5      6 
     7      8      9 

    42     42     42 
    42  -4242     42 
    42     42     42 

It seems there is a type.好像有一种。 You mean either the structure struct matrices or the structure struct matrix in the declarations of the code snippet.您指的是代码片段声明中的结构struct matrices或结构struct matrix

If to ignore the typo the initialization will look like如果忽略错字,初始化看起来像

struct matrices {
     int matrix[3][3]; };

struct matrices creation (int x, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8 ) {
     struct matrices mmm = 
     { 
         {
             { x,  x1, x2 }, 
             { x3, x4, x5 }, 
             { x6, x7, x8 }
         } 
      };
     return mmm;
}

Or the following way或者以下方式

struct matrices {
     int matrix[3][3]; };
     
struct matrices creation(int x, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8 ) {
     struct matrices mmm = 
     {
        .matrix = 
        {
            {x, x1, x2}, {x3, x4, x5}, {x6, x7, x8}
        }           
     };
     
     return mmm;
}

That is the aggregate struct matrix contains a two dimensional array.即聚合结构矩阵包含一个二维数组。 So you need to use one more pair of braces.所以你需要再使用一对大括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM