[英]How do you make an array of structs in C?
我正在尝试制作一个结构数组,其中每个结构代表一个天体。
我对结构没有太多经验,这就是为什么我决定尝试使用它们而不是一大堆 arrays。 但是,我不断遇到许多不同的错误。 我尝试实现我在各种线程和 StackOverflow 上看到的技术(例如Array of structs in C和C - initialize array of structs ),但并非所有这些都适用。
对于那些已经读到这里的人的更多信息:我不需要任何这些都是动态的,我事先知道/定义了所有内容的大小。 我还需要将它作为一个全局数组,因为我在几种不同的方法中访问它,这些方法已经定义了 arguments(即 GLUT 方法)。
这就是我在 header 中定义结构的方式:
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
)。
我在几种不同的方法中使用这个数组,但最简单且占用空间最少的方法是我的 main.js 的简化形式。 在这里,我初始化每个结构中的所有变量,只是为了在我以某种方式修改它们之前设置变量:
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 行是我制作结构数组的位置。
最后一个澄清,header 我的意思是int main(void)
上方的空间,但在同一个*.c
文件中。
#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;
}
这很好用。 顺便问一下你的问题不是很清楚,所以要将源代码的布局与上面的内容相匹配。
我想你也可以这样写。 我也是学生,所以我理解你的斗争。 有点迟到的反应但还可以。
#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];
所以使用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);
}
移动
struct body bodies[n];
之后
struct body
{
double p[3];//position
double v[3];//velocity
double a[3];//acceleration
double radius;
double mass;
};
休息一切看起来很好。
初始化结构数组的另一种方法是显式初始化数组成员。 如果没有太多的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成员,这种方法也很有用,它可以让代码阅读器更好地概述结构及其成员的目的和功能。应用。
该错误意味着编译器在声明结构数组之前无法找到结构类型的定义,因为您说您在头文件中定义了结构,并且错误在nbody.c
然后你应该检查你是否正确包含头文件。 检查你的#include
,并确保在声明该类型的任何变量之前完成结构的定义。
使用指针的解决方案:
#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;
}
您可以以与创建数字数组相同的方式执行此操作,但将元素的值包装在大括号中,如下所示 ->
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.\n", 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.