C 具有多个数组的结构,其大小在执行时定义

C 具有多个数组的结构,其大小在执行时定义,c,arrays,struct,C,Arrays,Struct,我想要的是这样的struct: struct Store{ int client_debts[]; struct items[]; }; struct Store s; int client_debts[defined_size]; s.client_debts = client_debts; 当执行开始时,程序读取一个输入文件,该文件定义了结构中数组的大小,因此我可以这样做: struct Store{ int client_debts[]; struct

我想要的是这样的
struct

struct Store{
    int client_debts[];
    struct items[];
};
struct Store s;
int client_debts[defined_size];
s.client_debts = client_debts;
当执行开始时,程序读取一个输入文件,该文件定义了
结构中数组的大小,因此我可以这样做:

struct Store{
    int client_debts[];
    struct items[];
};
struct Store s;
int client_debts[defined_size];
s.client_debts = client_debts;
我怎样才能做到这一点


PD:我尝试在
struct
中使用指针,然后将数组分配给它们,但是当创建数组的函数结束时,阵列内存被释放,因此指针一直指向未分配的内存,从而产生分段错误。

通常的方法是动态分配它们:

struct Store{
    int             *client_debts;
    struct mystruct *items;
};

struct Store store;
int n;

scanf ("%d", &n);
if (n <= 0)
   error_message();

store.client_debts = malloc (sizeof (*store.client_debts) * n);
store.items        = malloc (sizeof (*store.items) * n);
if (!store.client_debts  ||  !store.items)
   error_message();
struct存储{
int*客户债务;
struct mystruct*项;
};
结构商店;
int n;
scanf(“%d”和“&n”);

如果(n)您应该使用dynamic mem来执行此操作,那么dynamic mem永远不会超出范围,因此在您执行此操作之前,它将永远不会被释放。可能您的意思是sizeof(int)*n?;与其他变量相同……甚至
sizeof(*store.client\u)*n
@giorgi:no.我写它的方式对修改过程中引入的错误具有最大的抵抗力。如果这些字段的类型被更改了怎么办?@WeatherVane:是的。谢谢!谢谢!这正是我需要的!:D