I've been working on a threads lately and I have a question here. Here is a code snippet I have:
pthread_create(&thread[i], NULL, hello, (void *) i);
And in the thread hello, I print the value i that I have passed on from the above.
void *hello(void *n)
{
printf( " %d " , n);
pthread_exit(NULL);
}
The question is: While there is a need for me to typecast i to (void *), why can I succesfully print out the i without having to recast it back to int?
You are probably on a 32 bit machine where pointers and int
have the same width. So it is pure coincidence that this prints well.
This is really bad coding style, buys you a lot of trouble if you want to port your code, but gives you no gain at all. Just don't.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.