C question
Hi,
I am passing a double pointer to a function to allocate a double array and initialise the array inside the function with lets say 10.10;
I do the following but get segmentation fault when I access the array in main;
void function(double **array, int size){
*array = (double*) malloc(size * sizeof(double));
int i;
for(i=0;i<size;i++){
*array[i] = 10.10;
}
}
int main(){
double *array = NULL;
function(&array,20);
printf("array[0] = %lg\n",array[0]);// here is where I get segmentation fault
}
Any help ?
doesn’t mean what you think it does (look it up using a C operator precedence table).
Instead of unreadable, ugly and confusing (yes, it just confused you) code, use a temporary variable (and do not for the love of God cast the return value of malloc!):
Also,
return 0;frommain(). Really.