Ulfric Storm Ulfric Storm - 3 months ago 16
C Question

How to return an floating point array

I need a function that creates an array with some floating points.

double * my_function( )
{
static double arr[10] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29};

return arr;
}


int main ()
{
double *first_pos;
int i;

first_pos = my_function();
for ( i = 0; i < 10; i++ )
{
printf( "%d", *(first_pos + i));
}

return 0;
}


This prints some "random" numbers.

I'm a confused about pointers/arrays!

Answer

Your pointer/array usage is fine.

printf("%f", *(p + i));

Print doubles with the %f specifier. %d is for ints.

Comments