[英]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.