0

I have been learning about pthread library, and have created a simple code to multiply two numbers, however I'm not able to get rid of this warning. Ps. The code works fine.

#include <stdio.h>
#include <pthread.h>

struct numbers {
   int num1,num2;
};

void *mult(void *param) {
   struct numbers *data = param;
   int res = data->num1 * data->num2;
   pthread_exit((void *)res);
}

int main(){

   pthread_t tid;
   struct  numbers n;
   n.num1 = 2;
   n.num2 = 3;
   pthread_create(&tid, NULL,mult, (void*)&n);
   int res;
   pthread_join(tid, (void *)&res);
   printf("%d\n",(int)res);
   return 0;
}

Here's the warning:

1.c: In function ‘mult’:
1.c:12:17: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   12 |    pthread_exit((void *)res);

Any insights would be highly appreciated.

Shreyas Shrawage
  • 340
  • 2
  • 10
  • 1
    If you include ``, change res to be an `intptr_t`; it should shut up your compiler. Your compiler is just being a karen. – mevets Nov 27 '20 at 17:15

1 Answers1

1

change

pthread_exit((void *)res);

to

pthread_exit((void *)&res);
Harry
  • 2,177
  • 1
  • 19
  • 33
  • In the original posted code `res` is an auto; see the NOTES section for [pthread_exit()](https://man7.org/linux/man-pages/man3/pthread_exit.3.html) – Milag Nov 27 '20 at 15:42
  • 1
    Don't do that, that is quite wrong. If you are lucky your program will fault; but normally this just propagates invalid data. – mevets Nov 27 '20 at 17:11
  • @mevets is right about this. I have tried your solution before, it gets rid of the warning although provides us with a wrong answer. – Shreyas Shrawage Nov 29 '20 at 13:00