Void * pointer Origin

Source: Internet
Author: User
[Reprint] http://blog.csdn.net/mhjcumt/article/details/7355127

Int a = 1;

Int * p = &;

Float * p1 = (float *) p;

The values of p and p1 are both & a, but * p interprets the values in & a according to the int type variable, * p1 interprets the value in & a according to the float variable.


Given the need for flexible forced type conversion between pointers and the need for simplified code, ansi c introduces a null pointer, namely void *. Void pointers are also called Universal pointers. In many programs, they are replaced by universal pointers when the parameters are uncertain. Such pointers are particularly common in thread/process functions.

Ansi c stipulates that the void pointer can be copied to any type of pointer, and other types of pointers can also be copied to the void pointer. The forced type conversion is not required for copying between them. Of course, any address can also be copied to the void pointer. We often see accept (socket,
(Struct sockaddr *) & saddr_c, & lenth) You need to add code (struct
Sockaddr *) is because when this function is designed, ansi c has not introduced the concept of void. All addresses use struct
Sockaddr type identifier. The second parameter of this function is also a pointer to the struct sockaddr type, which is forced type conversion.

Of course, in some compilers, pointers of different types can also be directly assigned values, but in general, a warning of Type mismatch is given. Requiring the programmer to display a pointer to force type conversion can remind the programmer to use the pointer with caution, which has certain benefits for clarifying the purpose of the program.

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.