I'm working on socket programming.. my code executes the way I want it to, I'm able to use it. BUT it gives me a warning on compilation.
I compile using
gcc server1.c -o server1 -lpthread
And I get the warning
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
This error comes for the following code
int newsockfd;
newsockfd = (int)newsockfdd; //this line
And I'm using newsockfdd (which is int) in the following chunk of code
if (pthread_create(&threadID[i++], NULL, serverThread, (void *)(intptr_t)newsockfdd) != 0)
{
perror("Thread create error");
}
As you can probably tell, the code is not written too well (I am working on making it better). I know that this warning comes because of something to do with the size of int. But I really dunno how to fix it. Before I put in (intptr_t) in the pthread_create statement, it was showing a warning on that line, but that time the warning was
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
It seems like there should be a simple fix to this? But I can't find it. I'm using Ubuntu 64-bit. Is that a reason for the warning?
(void*)newsockfddbefore you interposed the cast tointptr_t, it seems unlikely that thecast from pointer to integer of different sizewarning is actually from the indicated line.serverThreadandnewsockfddis the argument thatserverThreadreceives? Then it would be clear.pthreadtakes avoid*as argument. So yourintis first cast tovoid*when it is passed, then inserverThread, thevoid*is cast back toint. It's bad style, bad probably will work. But, if possible, pass a pointer tonewsockfddand inserverThread, cast that pointer toint*and dereference it.