Returning array in C
Hello everyone!
I've been away from low level statement in C for a while. I'm having trouble with a function that should return an array of numbers.
1 2 3 4 5 6 7 8 9
|
double * f(s)
{
double * p = (double **) malloc(a * b * sizeof(double));
// operation on the array
return p;
}
|
The program crashes here although I have declared the variable that f returns to as
double *. Now, I know I can do this
1 2 3 4 5
|
void f(double * p, s)
{
// operation on the array
}
|
But I prefer using the first one.
Any help is appreciated!
|
double * p = (double **) malloc(a * b * sizeof(double));
|
modify to:
|
double * p = (double *) malloc(a * b * sizeof(double));
|
i wonder how come the compiler didnt throw an error.
Topic archived. No new replies allowed.