[英]How to set size of nested structure?
I'd like to make a simple database without making a dynamic sized array. 我想制作一个简单的数据库而不制作一个动态大小的数组。 I thought nested structure can help me, but so far it's nothing but pain.
我以为嵌套结构可以帮助我,但是到目前为止,除了痛苦之外,什么都没有。 This was my idea when I started:
当我开始时,这就是我的想法:
#include <stdio.h>
#define MAXDOG 50
#define MAXCHAR 20
struct allDog {
int size;
struct oneDog[MAXDOG] {
char dogName[MAXCHAR];
char OwnerName[MAXCHAR];
};
};
I'm pretty sure that my problem is the [MAXDOG] part after struct oneDog, can I give a static size somehow to the inner structure? 我很确定我的问题是struct oneDog之后的[MAXDOG]部分,我能以某种方式给内部结构一个静态大小吗?
You can read the part of the exam I try to do below: 您可以在下面阅读我尝试做的部分考试:
The program asks for the Dog's & Owner's name (MAX 50) then prints out the data. 程序会询问“狗的主人”的名字(最大为50),然后打印出数据。 You can't use dynamic arrays...
您不能使用动态数组...
You're overcomplicating things - try this: 您使事情变得过于复杂-试试这个:
#define MAXDOG 50
#define MAXCHAR 20
typedef struct { // struct which defines one dog
char dogName[MAXCHAR];
char ownerName[MAXCHAR];
} Dog;
typedef struct { // struct which contains array of dogs
int size; // no of dogs in array currently
Dog dogs[MAXDOGS]; // array of up to MAXDOGS dogs
} Dogs;
You sized the type not the member, syntax is: 您确定类型的大小而不是成员大小,语法为:
struct allDog { // type
int size;
struct oneDog { // type
char dogName[MAXCHAR];
char OwnerName[MAXCHAR];
} dogs[MAXDOG]; // member
};
Take care to be consistent with caps in naming, member OwnerName
should be written ownerName
to be consistent with other members. 请注意与命名上限保持一致,成员
OwnerName
应该写为ownerName
以与其他成员保持一致。 Types are generally typed AllDog
and OneDog
, to differentiate in between members and types. 通常将类型键入
AllDog
和OneDog
,以区分成员和类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.