gpt4 book ai didi

c - C中结构中的动态结构数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:59 24 4
gpt4 key购买 nike

我是 C 编程的新手,我无意中遇到了一些棘手的问题。我想做的是在 C 中的另一个结构中定义一个动态结构数组。

这是我的代码:首先我定义结构形状:

struct cap_prof_arrays
{
double zarr;
double profil;
double sx;
double sy;
double d_arr;
};

struct cap_profile
{
int nmax;
double rtot1;
double rtot2;
double cl;
double binsize;
struct cap_prof_arrays arr[]; /*Will get proper size allocated to it later*/
};

void read_cap_profile(struct inp_file cap) /* I defined the inp_file structure cap before, everything works great on that account */
{
FILE *fptr;
int i, n_tmp;
struct cap_profile profile;

fptr = fopen(cap.prf,"r");
fscanf(fptr,"%d",&profile.nmax);
// Increase structure array sizes to fit demands
profile.arr = malloc(sizeof(struct cap_profile)+profile.nmax*sizeof(double));
//continue reading data
for(i=0; i<=profile.nmax; i++){
fscanf(fptr,"%lf %lf",&profile.arr[i].zarr,&profile.arr[i].profil);
}
fclose(fptr);

//rest of program

现在,主要问题是在编译过程中,无论我尝试什么,我都会在尝试执行内存分配的行中遇到一些错误。我想知道你们中是否有人可以帮助我?我意识到我可以通过在结构声明期间定义一个非常大的数组大小来让我的生活变得更轻松,但我很乐意通过动态调整大小来做到这一点。所以我希望你能帮助我:)

非常感谢!

编辑 1:所以我根据我在这里得到的答案改变了一些东西。这是我的程序现在的样子:

struct cap_prof_arrays
{
double zarr;
double profil;
double sx;
double sy;
double d_arr;
};

struct cap_profile
{
int nmax;
double rtot1;
double rtot2;
double cl;
double binsize;
struct cap_prof_arrays *arr; /* will get proper size allocated to it later */
};

struct cap_profile read_cap_profile(struct inp_file cap)
{
FILE *fptr;
int i, n_tmp;
// struct cap_profile profile;

printf("Reading capillary profiles...\n");
fptr = fopen(cap.prf,"r"); /* READING THE CAPILLARY PROFILE */
if(fptr == NULL){
printf("%s file does not exist.\n",cap.prf);
exit(0);
}
fscanf(fptr,"%d",&n_tmp);
// increase structure array sizes to fit demands;
struct cap_profile *profile = malloc(sizeof(*profile)+(n_tmp+1)*sizeof(*profile->arr));
profile->nmax = n_tmp;
// continue reading the data;
for(i=0; i<=profile->nmax; i++){
fscanf(fptr,"%lf %lf",&profile->arr[i].zarr,&profile->arr[i].profil);
}
fclose(fptr);

n_tmp = profile->nmax;

fptr = fopen(cap.axs,"r"); /* READING THE AXIS DEFINITION FILE */
if(fptr == NULL){
printf("%s file does not exist.\n",cap.axs);
exit(0);
}
fscanf(fptr,"%d",&profile->nmax);
if(profile->nmax != n_tmp){
printf("Inconsistent axis.dat file: number of intervals different.\n");
exit(0);
}
for(i=0; i<=profile->nmax; i++)
fscanf(fptr,"%lf %lf %lf",&profile->arr[i].zarr,&profile->arr[i].sx,&profile->arr[i].sy);
fclose(fptr);

fptr = fopen(cap.out,"r"); /* READING THE OUTER PROFILE */
if(fptr == NULL){
printf("%s file does not exist.\n",cap.out);
exit(0);
}
for(i=0; i<=profile->nmax; i++)
fscanf(fptr,"%lf %lf",&profile->arr[i].zarr,&profile->arr[i].d_arr);
fclose(fptr);

profile->rtot1 = profile->arr[0].d_arr;
profile->rtot2 = profile->arr[profile->nmax].d_arr;
profile->cl = profile->arr[profile->nmax].zarr;
cap.d_screen = cap.d_screen + cap.d_source + profile->cl;
profile->binsize = 20.e-4;

return *profile;
}

我现在得到的错误是 Segmentation fault (core dumped) 错误(在程序测试期间,而不是在编译期间)。我想我的指针又做错了,但我真的不明白我做错了什么……有什么帮助吗?

最佳答案

您不能在与结构分开的结构内分配数组。相反,对于一个灵活的数组成员*,这样做:

将要分配的元素数量读取到一个 int 中,也许是一个名为 nmax 的元素(一个单独的 int 本身,而不是nmax 结构中的成员)。

为整个结构和数组分配空间,并将该空间的地址分配给指针:

struct cap_profile *profile = malloc(sizeof *profile + nmax * sizeof *profile->arr);

* 灵活的数组成员是在结构末尾具有未指定主维度的数组。

关于c - C中结构中的动态结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com