I created a thread that should return a sqrt of integer sent to it, it works fine while returning int value, but when i want to return double or float value it returns some crazy numbers, how to change that?
Here's the code that works fine:
int* function(int* x) {
printf("My argument: %d \n", *x);
int *y = malloc(sizeof(int));
*y=sqrt(*x);
return y;
}
int main(int argc, char* argv[])
{
pthread_t thread;
int arg = 123;
int *retVal;
pthread_create(&thread, NULL, (void * ( * ) (void *))function, &arg);
pthread_join(thread, (void **) &retVal);
printf("Sqrt of our argument: %d\n", * retVal);
free(retVal);
return 0;
}
But when I change it into:
double* function(int* x) {
double *y = malloc(sizeof(double));
*y=sqrt(*x);
printf("My argument: %d \n", *x);
return y;
}
int main(int argc, char* argv[])
{
pthread_t thread;
int arg = 123;
double *retVal;
pthread_create(&thread, NULL, (void * ( * ) (void *))function, &arg);
pthread_join(thread, (void **) &retVal);
printf("Sqrt of our argument: %d\n", * retVal);
free(retVal);
return 0;
}
It returns 1076244058