你如何在 C 中创建一个结构数组? [英] How do you make an array of structs in C?

查看:31
本文介绍了你如何在 C 中创建一个结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an array of structs where each struct represents a celestial body.

我对结构没有太多经验,这就是为什么我决定尝试使用它们而不是一大堆数组.但是,我不断遇到许多不同的错误.我试图实现我在各种线程和 StackOverflow 上看到的技术(例如 C 中的结构数组C - 初始化结构数组),但并非所有这些都适用.

I don't have that much experience with structs, which is why I decided to try to use them instead of a whole bunch of arrays. However, I keep on running into numerous different errors. I've tried to implement the techniques that I've seen on various threads and on StackOverflow (such as Array of structs in C and C - initialize array of structs), however not all of them were applicable.

给那些读到这里的人的更多信息:我不需要任何这些都是动态的,我事先知道/定义了所有东西的大小.我还需要它是一个全局数组,因为我在几种不同的方法中访问它,这些方法已经定义了参数(即 GLUT 方法).

Further information for those who have read this far: I don't need any of this to be dynamic, I know/define the size of everything beforehand. I also need this to be a global array as I'm accessing this in several different methods which have defined arguments (i.e. GLUT methods).

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

This is how I'm defining the struct in my header:

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

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

I have a list of other global variables that I'm defining before I define the interior of the struct, and one of those is the array of this struct (basically, if I'm being too unclear in my fogged speak, the line below is above the stuff above):

struct body bodies[n];

请注意,n 是我合法定义的(即 #define n 1).

Just so you know, n is something that I've legitimately defined (i.e. #define n 1).

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

I use this array in several different methods, but the easiest and least space consuming one is a simplified form of my main. Here I initialize all of the variables in each of the structs, just to set the variables for certain before I modify them in some way:

  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 not completed element type 其中第 32 行是我制作结构数组的地方.

The current error that I'm facing is nbody.c:32:13: error: array type has incomplete element type where line 32 is where I'm making the array of the structs.

最后一个澄清,标题是指 int main(void) 上方的空格,但在同一个 *.c 文件中.

One last clarification, by header I mean the space above int main(void) but in the same *.c file.

推荐答案

#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;
}

这很好用.顺便说一下,您的问题不是很清楚,因此请将您的源代码布局与上述内容相匹配.

this works fine. your question was not very clear by the way, so match the layout of your source code with the above.

这篇关于你如何在 C 中创建一个结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆