你如何结构的C中的数组? [英] How do you make an array of structs in C?

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

问题描述

我试图使结构的数组,每个结构重新presents一个天体的,我在我的班级工作的问题。我没有与结构的很多经验,这就是为什么我决定尝试使用它们替代了一大堆阵列的,但是,我一直运行到许多不同的错误,尽管我试图实现这些技术我已经看到了不同的线程和计算器(如在使用C 阵列一>和ç - 初始化结构的数组的),但他们并不是所有的人适用,所以我没能做到这一点的方式完全复制。就在之前,预警我告诉你什么是我想要做什么,我将无法回复评论/提问/回答了好几个小时,因为我需要去睡觉,因为我一直醒着的时间太长了,对此,我感到非常抱歉,但我有过今天很忙,几个小时在这个问题上已经工作后,真的累了。

对于那些谁读到这里进一步的信息:我不需要任何这是动态的,我知道/事先定义所有内容的大小。我还需要这是一个全球性的阵列(喘气全局变量)因为我在已定义的参数(即葡萄糖转运方法)几种不同的方法访问这一点。

这是我如何定义我的头的结构:

 结构体
{
    双P [3]; //位置
    双V [3]; //速度
    加倍[3]; //加速度
    双半径;
    双质量;
};

我有我定义之前,我定义结构内部的其它全局变量列表,其中之一是这个结构数组(基本上,如果我是在我的雾说得太清楚,下面的线是上方的东西以上):

 结构体机构[N];

只要你知道, N 是我合法定义的东西(即的#define N + 1 ) 。

我用这个数组中的几种不同的方法,但耗时一个最简单,最空间是我的主要的简化形式,我初始化所有的变量在每个结构,只需设置变量某些以前我修改他们以某种方式:

  INT A,B;
 用于:(a = 0;一个与所述; N;一++)
 {
        对于(B = 0; B< 3; b ++)
        {
            体[α] .P并[b] = 0;
            体[α] .V并[b] = 0;
            体[α] .A并[b] = 0;
        }
        体[α] .mass = 0;
        体[α] .radius = 1.0;
 }

这是我面对当前的误差 nbody.c:32:13:错误:数组类型具有不完整的元素类型,其中第32行就是我正在做阵列的结构的。

感谢您的任何和所有帮助你屈尊给,我承诺,我会尽快给你了最新的12时间从现在。

最后一个澄清,以头我的意思是上面 INT主要(无效)空间但在相同的* .c文件。


解决方案

 #包括LT&;&stdio.h中GT;
#定义ñ3
车身结构
{
    双P [3]; //位置
    双V [3]; //速度
    加倍[3]; //加速度
    双半径;
    双质量;
};结构体的身体[n]的;诠释的main()
{
    诠释A,B;
     用于:(a = 0;一个与所述; N;一++)
     {
            对于(B = 0; B< 3; b ++)
            {
                体[α] .P并[b] = 0;
                体[α] .V并[b] = 0;
                体[α] .A并[b] = 0;
            }
            体[α] .mass = 0;
            体[α] .radius = 1.0;
     }    返回0;
}

这工作正常。你的问题是不是对了明确得多,因此与上述匹配源$ C ​​$ C的布局。

I'm trying to make an array of structs where each struct represents a celestial body for the problem that I'm working on in my class. 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, even though I've tried to implement the techniques that I've seen on various threads and on stackoverflow (such as at array of structs in C and C - initialize array of structs), however not all of them were applicable, so I wasn't able to completely copy the way to do it. Just a forewarning before I show you what I'm trying to do, I won't be able to reply to comments/questions/answers for several hours as I need to go to bed since I've been awake for far too long, I'm truly sorry about this, but I'm really tired after having had a busy day and having worked on this problem for several hours.

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 (gasp GLOBAL VARIABLES) 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];

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

I use this array in several different methods, but the easiest and least space consuming one is a simplified form of my main where 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;
 }

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.

Thank you for any and all help that you deign to give, I promise that I will get back to you at the very latest, 12 hours from now.

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 much clear by the way, so match the layout of your source code with the above.

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

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