首页 文章

你如何在C中创建一个结构数组?

提问于
浏览
82

我正在尝试制作一个结构数组,其中每个结构代表一个天体 .

我没有试图实现我在各种线程和StackOverflow上看到的技术(例如Array of structs in CC - initialize array of structs),但并非所有技术都适用 .

对于那些已经阅读过这一点的人的进一步信息:我不需要任何这些是动态的,我知道/事先确定一切的大小 . 我还需要将它作为一个全局数组,因为我在几个不同的方法中访问它,这些方法已经定义了参数(即GLUT方法) .

这就是我在 Headers 中定义结构的方式:

struct body
{
    double p[3];//position
    double v[3];//velocity
    double a[3];//acceleration
    double radius;
    double mass;
};

在定义结构内部之前,我有一个其他全局变量的列表,其中一个是这个结构的数组(基本上,如果我在模糊的说话中太不清楚,下面这一行高于上面的东西):

struct body bodies[n];

只是你知道, n 是我合法定义的东西(即 #define n 1 ) .

我在几种不同的方法中使用这个数组,但最容易和最少占用空间的是我的主要简化形式 . 在这里,我初始化每个结构中的所有变量,只是为了在我以某种方式修改它们之前设置变量:

int a, b;
 for(a = 0; a < n; a++)
 {
        for(b = 0; b < 3; b++)
        {
            bodies[a].p[b] = 0;
            bodies[a].v[b] = 0;
            bodies[a].a[b] = 0;
        }
        bodies[a].mass = 0;
        bodies[a].radius = 1.0;
 }

我面临的当前错误是 nbody.c:32:13: error: array type has incomplete element type ,其中第32行是我正在构建结构数组的地方 .

最后一个澄清,通过 Headers 我的意思是 int main(void) 之上的空间,但在同一个 *.c 文件中 .

7 回答

  • 0
    #include<stdio.h>
    #define n 3
    struct body
    {
        double p[3];//position
        double v[3];//velocity
        double a[3];//acceleration
        double radius;
        double mass;
    };
    
    struct body bodies[n];
    
    int main()
    {
        int a, b;
         for(a = 0; a < n; a++)
         {
                for(b = 0; b < 3; b++)
                {
                    bodies[a].p[b] = 0;
                    bodies[a].v[b] = 0;
                    bodies[a].a[b] = 0;
                }
                bodies[a].mass = 0;
                bodies[a].radius = 1.0;
         }
    
        return 0;
    }
    

    这很好用 . 顺便问一下你的问题不是很清楚,所以要将源代码的布局与上面的内容相匹配 .

  • 8

    我想你也可以这样写 . 我也是学生,所以我理解你的斗争 . 有点迟到的反应但还可以 .

    #include<stdio.h>
    #define n 3
    
    struct {
        double p[3];//position
        double v[3];//velocity
        double a[3];//acceleration
        double radius;
        double mass;
    }bodies[n];
    
  • 10

    移动

    struct body bodies[n];
    

    之后

    struct body
    {
        double p[3];//position
        double v[3];//velocity
        double a[3];//acceleration
        double radius;
        double mass;
    };
    

    休息一切看起来很好 .

  • 90

    所以使用 malloc() 把它们放在一起:

    int main(int argc, char** argv) {
        typedef struct{
            char* firstName;
            char* lastName;
            int day;
            int month;
            int year;
    
        }STUDENT;
    
        int numStudents=3;
        int x;
        STUDENT* students = malloc(numStudents * sizeof *students);
        for (x = 0; x < numStudents; x++){
            students[x].firstName=(char*)malloc(sizeof(char*));
            scanf("%s",students[x].firstName);
            students[x].lastName=(char*)malloc(sizeof(char*));
            scanf("%s",students[x].lastName);
            scanf("%d",&students[x].day);
            scanf("%d",&students[x].month);
            scanf("%d",&students[x].year);
        }
    
        for (x = 0; x < numStudents; x++)
            printf("first name: %s, surname: %s, day: %d, month: %d, year: %d\n",students[x].firstName,students[x].lastName,students[x].day,students[x].month,students[x].year);
    
        return (EXIT_SUCCESS);
    }
    
  • 1

    该错误意味着编译器在声明结构数组之前无法找到结构类型的定义,因为您说您在头文件中定义了结构,并且错误在 nbody.c 中那么你应该检查你是否正确包含头文件 . 检查你的 #include 并确保在声明该类型的任何变量之前完成结构的定义 .

  • 8

    使用指针的解决方案:

    #include<stdio.h>
    #include<stdlib.h>
    #define n 3
    struct body
    {
        double p[3];//position
        double v[3];//velocity
        double a[3];//acceleration
        double radius;
        double *mass;
    };
    
    
    int main()
    {
        struct body *bodies = (struct body*)malloc(n*sizeof(struct body));
        int a, b;
         for(a = 0; a < n; a++)
         {
                for(b = 0; b < 3; b++)
                {
                    bodies[a].p[b] = 0;
                    bodies[a].v[b] = 0;
                    bodies[a].a[b] = 0;
                }
                bodies[a].mass = 0;
                bodies[a].radius = 1.0;
         }
    
        return 0;
    }
    
  • 0

    初始化结构数组的另一种方法是显式初始化数组成员 . 如果没有太多的struct和array成员,这种方法很有用而且很简单 .

    使用 typedef 说明符可避免每次声明struct变量时重复使用 struct 语句:

    typedef struct
    {
        double p[3];//position
        double v[3];//velocity
        double a[3];//acceleration
        double radius;
        double mass;
    }Body;
    

    然后声明你的结构数组 . 每个元素的初始化都伴随着声明:

    Body bodies[n] = {{{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0}, 
                      {{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0}, 
                      {{0,0,0}, {0,0,0}, {0,0,0}, 0, 1.0}};
    

    重复一遍,如果您没有太多的数组元素和大型结构成员,并且如您所说,如果您对更动态的方法不感兴趣,那么这是一个相当简单和直接的解决方案 . 如果使用命名的枚举变量(而不仅仅是上面的例子中的数字)初始化struct成员,这种方法也很有用,它可以让代码阅读器更好地概述结构及其成员的目的和功能 . 应用 .

相关问题