C-language void* pointers using __c language

Source: Internet
Author: User
#include <stdio.h>
#include <stdlib.h>
   
//void * Can be any type of data, any type can be stored, or you can convert any type
int main () {
	//Example 1: Assign the normal pointer variable to the void* pointer and print out the
	int pa =;
	int *p = &pa;
	void * v = p; 
	printf ("v=%d \", * ((int*) v));
	
	Example 2: Modifying the value of a variable by void* pointer
	float f = 3.14f; 
	void *v2 = &f;
	printf ("f=%.2f,v2=%.2f\n", f,* (float*) v2);/Output f=3.14,v2=3.14
    * ((float*) v2) = 100.99f;//after
    modifying values by void* pointer printf ("f=%.2f,v2=%0.2f\n", f,* (float*) v2));	
	Output f=100.99,v2=100.99
	
	system ("pause");
	return 0;
}



/*
Note: In the ANSI C standard, some arithmetic operations on void pointers, such as p++ or p+=1, are not allowed.
Because void is no type, we don't know how many bytes to operate on every arithmetic operation.
But GNU is not so determined, it specifies void * 's algorithm operation and char * consistent. The following statements are therefore correct in the GNU compiler:
pvoid++; GNU: Correct pvoid = 1; GNU: The correct pvoid++ result is that it's increased by 1
*/

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.