Hi,
I am developing a large scale code. In order to save the space, when I declare any pointer, I allocate it in another subroutine. Now it seems that it does not work when I use an array of pointers. I can summarize the code as:
#include "headers.h"
/** Object of extrapolation coefficients **/
typedef struct{
double *zero[2];
double *one [2];
}prt_interpln;
int main(){
/** Declare functions **/
double *interp(const int, const int );
/** Decalre variables **/
const int length = 4;
int i;
double y;
/** Declare pointer **/
prt_interpln *interp_coeff;
interp_coeff->zero[0] = interp(length, 0);
interp_coeff->zero[1] = interp(length, 1);
interp_coeff->one [0] = interp(length, 2);
interp_coeff->one [1] = interp(length, 3);
/****************************/
for(i = 0; i < length; i++){
y = interp_coeff->one [1][i];
printf("Y(%d) = %f\n", i, y);
}
/****************************/
free( interp_coeff );
return 0;
}
double *interp( const int length, const int agent ){
int i;
double x;
double *y = malloc(length * sizeof(double)) ;
/*********/
for(i = 0; i < length; i++){
x = (double) agent * i;
y[i] = x ;
}
/** End **/
return y;
}
if I do so, I get the following warning:
pointer.c: In function ‘main’:
pointer.c:39: warning: ‘interp_coeff’ is used uninitialized in this function
If I run it I get segmentation fault. If I allocate memory for interp_coeff inside main, then it works.
But I think then I am doing somthing wrong! Because the interp_coeff should be allocated once inside main and another time inside the routine
double *interp( const int length, const int agent )
It seems like a conflict!!
Please help!
Thanks in advance