I'm quite new to C programming, and I have stumbled on some tricky problem. What I want to do is define a dynamic structure array in another structure in C.
Here is my code: First I define the structure shapes:
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
Now, the main problem is that during compilation I get some errors on the line where I try to perform the memory allocation, no matter what I try. I was wondering if any of you could help me out? I realize I could probably make my life a lot easier by just defining a very large array size during structure declaration, yet I would love to do it with the dynamic sizing. So I hope you can help me out :)
Thanks a lot!
EDIT1: So I changed some things based on answers I got here. This is what my program looks like now:
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;
}
The error I get now is a Segmentation fault (core dumped) error (during program testing, not during compilation). I suppose I'm doing something wrong with the pointers again, but I don't really understand what I'm doing wrong... Any help?