English 中文(简体)
你们如何在C中制造一系列障碍?
原标题:How do you make an array of structs in C?

我试图制造一系列的构件,每个构件代表一个天体。

我没有这方面的经验,这就是为什么我决定尝试使用这些结构,而不是整块阵列。 然而,我仍然有无数不同的错误。 I ve试图采用我从各个深层和Stack Overflow(例如)上看到的技术。 C>>在C>/>>>和的系列构件中,但并非全部都适用。

至今读过这段话的人的进一步信息:我不需要任何这种信息是动态的,我事先知道/弄清了一切的规模。 我还需要成为一种全球阵列,因为在界定了论点的不同方法中,Im获得这一许可(即GLUT方法)。

这就是我如何在我的头脑中确定方向:

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

目前Im正面临的错误是n.c:32:13:误差:阵列类型不完整,内容类型为,其中第32条线是使构件阵列的Im。

最后一项澄清: 页: 1

最佳回答

使用:

#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.

问题回答

启动一系列结构的另一个办法是明确启动阵列成员。 如果存在太多的障碍和阵列成员,这种做法是有用和简单的。

使用<代码>tdefspecifier,以避免重新使用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}};

再说一遍,如果你没有太多的阵列要素和大量固定成员,如果你说不关心更动态的做法,这是一个非常简单和直截了当的解决办法。 如果分阶段成员以名数的变量(而不仅仅是上面的例子)来初步形成,这种办法也是有益的,这样,它就能够使密码更全面地了解一个结构及其成员在某些应用中的宗旨和职能。

通过使用<条码>小型(<>>>>/代码”将其统一起来:

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
",students[x].firstName,students[x].lastName,students[x].day,students[x].month,students[x].year);

    return (EXIT_SUCCESS);
}

我认为也可以这样写。 我也是学生,因此我理解你的斗争。

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

问 题

struct body bodies[n];

页: 1

struct body
{
    double p[3]; // Position
    double v[3]; // Velocity
    double a[3]; // Acceleration
    double radius;
    double mass;
};

其余部分都属于罚款。

你可以采取与你创造一系列数字相同的方式这样做,但将要素的数值总结成像这种——和”;

struct Wrestler studs[count] = {
        {"John", "Cena"},
        {"The", "Undertaker"},
        {"The", "Big Show"},
        {"The", "Rock"},
        {"Triple", "H"},
        {"Scott", "Hall"},
        {"Roman", "Reings"},
        {"Dean", "Ambrose"}};

此处为正式法典

#include <stdio.h>

struct Wrestler
{
    char firstName[20];
    char secondName[20];
};

void pIntro(struct Wrestler *s)
{
    printf("Hi, I am %s %s.
", s->firstName, s->secondName);
};

int main(int argc, char const *argv[])
{
#define count 8
    struct Wrestler studs[count] = {
        {"John", "Cena"},
        {"The", "Undertaker"},
        {"The", "Big Show"},
        {"The", "Rock"},
        {"Triple", "H"},
        {"Scott", "Hall"},
        {"Roman", "Reings"},
        {"Dean", "Ambrose"}};

    for (int i = 0; i < count; i++)
    {
        pIntro(&(studs[i]));
    }

    return 0;
}


使用点人的解决办法:

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


    free(bodies);
    return 0;
}

That error means that the compiler is not able to find the definition of the type of your struct before the declaration of the array of structs, since you re saying you have the definition of the struct in a header file and the error is in nbody.c then you should check if you re including correctly the header file. Check your #include s and make sure the definition of the struct is done before declaring any variable of that type.

我在努力重建你的方案时,我开始

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

#define n 9

struct body bodies[n];

int main(void)
{
    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;
    }
}

该编码使用<条码>gcc-11-std=c17进行无错误汇编。 - Wall - Wextra - Wwrite-strings - 单亲家庭——Wpedantic - Warray-strict-prototypes -fanalyzer

然而,我们仍有机会通过使用一个固定的<条码>结构加以简化,以供初步使用:

int main(void)
{
    static const struct body default_body =
        { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, 0.0, 1.0 };

    for (unsigned i = 0;  i < n;  ++i) {
        bodies[i] = default_body;
    }
}

I don t know who came up with this one but. YES, you can build structs containing other structs! case in point work on a flight sim requires knowing start, finish, and current locations (Latitude and Longitude). An example that I use:

struct DM {
   int degrees;
   float minutes;
   char hemi; };

现在我需要其中两人到拉特,而另一人到隆;

struct DM Lat;
struct DM Lon;

现在,让它走到一起,称之为它的立场。

struct Position {
    struct DM Lat;
    struct DM Lon; } Current, Start, Finish;

and to access start I will use Addison, Texas // Addison: 32-58.113333N 096-50.186667W

Finish.Lon.degrees = 32;
Finish.Lon.minutes = 58.113333;
Finish.Lon.hemi    =  N ;
Finish.Lat.degrees = 96;
Finish.Lat.minutes = 0.186667;
Finish.Lat.hemi    =  W ; 

Fins ps Now what s all this crud about not make a ruct within a ?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...