我对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
现在,主要的问题是,在编译期间,无论我尝试什么,我都会在尝试执行内存分配的行上得到一些错误。我想知道你们谁能帮我一下?我意识到,如果在结构声明过程中定义一个非常大的数组大小,我的工作可能会轻松得多,但我还是喜欢使用动态调整大小。所以我希望你能帮助我:)
非常感谢!
EDIT1: 所以我根据这里的答案修改了一些东西。这是我的程序现在的样子:
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;
}
我现在得到的错误是分段错误(核心转储)错误(在程序测试期间,而不是在编译期间)。我想我又做错了指针,但我真的不明白我做错了什么……任何帮助吗?
不能在结构体内部单独分配数组。相反,对于一个灵活的数组成员*,这样做:
读取您想要分配给int
的元素数,可能是一个名为nmax
的元素(单独的int
本身,而不是结构体中的nmax
成员)。
为整个结构体和数组分配空间,并将该空间的地址分配给指针:
struct cap_profile *profile = malloc(sizeof *profile + nmax * sizeof *profile->arr);
*灵活数组成员是指位于结构体末尾的主维未指定的数组。
当你用malloc
(或类似的函数)分配内存时,它返回一个指针。不能将此指针赋值给数组。换句话说,一旦创建了数组,就不能让它指向其他对象。
你应该做的是将数组声明为指针。
你可以这样做:
struct cap_profile {
int nmax;
struct cap_prof_arrays **arr;
};
然后像这样分配
struct cap_profile *profile = calloc(1, sizeof(*profile));
fscanf(fptr, "%d", profile->nmax);
profile->arr = calloc(1, profile->nmax * sizeof(*(profile->arr)));
for (i = 0; i < profile->nmax; i++) {
profile->arr[i] = calloc(1, sizeof(*(profile->arr[i])));
fscanf(fptr, "%lf %lf", &profile->arr[i]->zarr, &profile->arr[i]->profil);
}
另外两点:
- 分配cap_profile结构并从read_cap_profile()函数返回指向它的指针,如果你想在该函数之外使用它的值。
- for循环应该有
nmax, not <=