void * NULL (none) type pointer

Source: Internet
Author: User
(void*) 4: Convert reshape 4 to address value 0x4

Today I wrote a program, I almost did not explain the pass.
Just started to write Pthread_exit (0); compile, run no problem
Written pthread_exit (4); Compile warning, run no problem.

Warning for the following reasons:

JOINTHREAD.C: In function ' Assisthread ':
jointhread.c:9:18: Warning: Assigning an integer to a pointer when passing the 1th argument of ' pthread_exit ', without type conversion [- Wint-conversion]
     pthread_exit (4);
                  ^
in file included from jointhread.c:4:0:
/usr/include/pthread.h:244:13: Note: Requires type ' void * ', but the type of the argument is ' int '
 extern void Pthread_exit (void *__retval) __attribute__ ((__noreturn__));

The main problem is the type, 0 is special, can be considered null.
Pthread_exit (void *__retval) requires a variable of type void *.
Pthread_exit ((void*) 4); change it to the right way.

FILE:JOINTHREAD.C
#include <stdio.h>     
#include <unistd.h>     
#include <stdlib.h>     
#include <pthread.h>     

void * Assisthread (void * arg) {    
    printf ("I am helping to do some jobs\n");    
    Sleep (3);    
    Pthread_exit ((void*) 4);    
}    

int main (int argc, const char *argv[])    
{    
    pthread_t assistthid;    
    int status;      

    Pthread_create (&assistthid, NULL, (void*) assisthread, null);      
    Pthread_join (Assistthid, (void*) &status);    
    printf ("Assistthread ' exit is cased%d\n", status);       
    return 0;    
}    

Run Result:
I am helping to do some jobs
assistthread ' exit is cased 4

Here's the question: (void*) 4 What a ghost.
The first is the type conversion, which we all know. is to convert 4 to address value: (void*) 4=0x4
And here I used (void*) &status to receive this address value (0x4)
The status variable itself has an address, assumed to be 8000, and the content of the status address (8000), with an address value (0x4)
Then, when it was printed with%d, the address value was converted to an integer 4.
Write a small example of yourself to understand:

 void *a;
    A= (void*) 2; printf ("(void*) 2=%p, a=%d\n", (void*) 2, a); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.