1

Though this seems to be a duplicate question, i could not find an answer that is marked correct. Also I could not find them helpful for my issue

Here is my code

void *testClassdMain(void *temp){
   initClassD((int)(temp),(char *)"localhost",10101,(char *)"localhost",10102);
   char txt[255]={0,};
   int len=0;

   while(1){
     fgets(txt,255,stdin);
     len=strlen(txt);
     ClassDSend((uint8_t *)txt, len);
   }
   return NULL;
 }

 int main(int argc, char *argv[]){

   int mode=0;

   if (signal(SIGINT, sig_handler) == SIG_ERR){
     printf("Cant catch\n");
     return -1;
   }

   if(!strcmp(argv[1],"server")){
     mode=1;
   }
   pthread_attr_init(&testAttr);
   pthread_attr_setdetachstate(&testAttr, PTHREAD_CREATE_JOINABLE);
   if(pthread_create(&testThrdId, &testAttr, testClassdMain, (void*)mode )){
     printf("Error in creating a thread\n");
     return -1;
   }
   pthread_exit(NULL);
   return 0;
 }

And while it is trying to create a thread its throwing following error

main.c: In function ‘void* testClassdMain(void*)’:
main.c:30: error: cast from ‘void*’ to ‘int’ loses precision

Please guide me

Thanks in Advance

user2256825
  • 594
  • 6
  • 22
  • 1
    Generally only use void* for casting between pointer types. – AndyG Jun 14 '16 at 11:33
  • 1
    Are you using C or C++? They are two different languages and C++ now has [built in thread support with type safety](http://en.cppreference.com/w/cpp/thread). – NathanOliver Jun 14 '16 at 11:34
  • Iam using C , Also would like to use it in c++ too. Currently its in c – user2256825 Jun 14 '16 at 11:35
  • You don't need to cast string literals to pointers to `char`. String literals will be added as compiler-generated arrays and like all arrays they decay to a pointer to their first element, which of course already is of type `char *`. – Some programmer dude Jun 14 '16 at 11:39
  • 1
    C11 draft at least defines `intptr_t` as a type that a `void*` can be cast to and back and the 2 pointers will compare equal. Not sure if this implies that intptr_t -> void* -> intptr_t holds the same. – Ilja Everilä Jun 14 '16 at 11:41
  • @Ilja Everilä It does not mean `intptr_t -> void* -> intptr_t` the initial and final integers compare equally. E.g. the range of `intptr_t` may exceed `void*`. – chux - Reinstate Monica Jun 14 '16 at 18:18

0 Answers0