Void * Explanation and Application in C Language

Source: Internet
Author: User

Void is interpreted as "empty, space, and gap" in English. In C, void is "no type" and the corresponding void * is "no type Pointer ". Void seems to have only "Comments" and restrictions on the role of the program. Of course, the "Comments" here are not for us, but for the compiler to provide a so-called comment.

URL: http://www.cnblogs.com/archimedes/p/c-void-point.html.

Void:

1. Restrictions on function return, which are common.

2. Restrictions on function parameters are also common.

These two situations are common:

When a function does not require a return value, you must use the void limitation. This is what we call the first case. For example, void func (int a, char * B ).

When a function does not accept parameters, you must use void. This is what we call the second case. For example, int func (void ).

Rules for using the void pointer:

1. The void pointer can point to any type of data, that is, the void pointer can be assigned a value to the void pointer with any type of pointer. For example:

Int *;

Void * p;

P =;

If you want to assign the void pointer p to a pointer of another type, you need to force type conversion. In this example, a = (int *) p. In memory allocation, we can see that the void pointer is used: the pointer returned by the memory allocation function malloc is the void * type. When using this pointer, You need to perform forced type conversion, that is, explicitly indicating the type of data (int *) malloc (1024) that the Pointer Points to in the memory) indicates that the void * pointer returned by malloc is forced to point to the memory to store int-type data.

2. in the ansi c standard, some arithmetic operations on the void pointer, such as p ++ or p + = 1, are not allowed because void is of no type, therefore, we do not know the number of bytes for each arithmetic operation. For example, the char type operation is sizeof (char), while the int type operation is sizeof (int. In GNU, it is allowed, because by default, GNU considers that void * is the same as char *. Since it is definite, of course some arithmetic operations can be performed. Here sizeof (* p) = sizeof (char ).

Void has almost only "Comments" and restrictions on the role of the program, because no one will ever define a void variable, let's try to define:

Void;

An error occurs when compiling this line of statements, and the message "illegal use of type 'void'" is displayed '". Even if the compilation of void a does not go wrong, it has no practical significance.

As we all know, if the pointer p1 and p2 are of the same type, we can directly assign values to each other between p1 and p2. If p1 and p2 point to different data types, you must use the forced type conversion operator to convert the pointer type on the right of the value assignment operator to the pointer type on the left.

Float * p1; int * p2; p1 = p2; // Where the p1 = p2 statement will cause compilation errors, // The prompt "'= ': cannot convert from 'int * 'to 'float *' ", must be changed to: p1 = (float *) p2;

Void * is different. pointers of any type can be directly assigned to it without forced type conversion.

void *p1;int *p2;p1 = p2;

However, this does not mean that void * can also be assigned to other types of pointers without force type conversion. Because "No type" can tolerate "type", while "type" cannot tolerate "No type ".

Be careful when using the void pointer type:

According to the ANSI (American National Standards Institute) standard, you cannot perform algorithm operations on the void pointer, that is, the following operations are invalid:

Void * pvoid; pvoid ++; // ANSI: Error pvoid + = 1; // ANSI: Error // The reason why the ANSI standard is determined as follows: the pointer to the algorithm operation must be determined to know the data type it points. // For example: int * pint; pint ++; // ANSI: The correct pint ++ result is to increase sizeof (int ).

However, GNU does not recognize this as it specifies that the void * algorithm operation is consistent with that of char. Therefore, the following statements are correct in the GNU Compiler:

Pvoid ++; // GNU: Correct pvoid + = 1; // GNU: The execution result of correct pvoid ++ is increased by 1.

In actual programming, to cater to ANSI standards and improve program portability, we can write code that implements the same function as below:

Void * pvoid; (char *) pvoid ++; // ANSI: Correct; GNU: Correct (char *) pvoid + = 1; // ANSI: error; GNU: Correct

There are some differences between GNU and ANSI. In general, GNU is more open than ANSI and provides support for more syntaxes. However, in actual design, we should try to cater to ANSI standards as much as possible. If the function parameter can be a pointer of any type, you should declare its parameter as void *

Note: The void pointer can be any type of data, which can bring us some benefits in the program. When the function is a pointer type, we can define it as a void pointer, in this way, the function can accept any type of pointer. For example:

Void literally means "no type", void * is "no type Pointer", and void * can point to any type of data.

Typical function prototypes for memory operation functions such as memcpy and memset are:

void * memcpy(void *dest, const void *src, size_t len);void * memset ( void * buffer, int c, size_t num );

In this way, any type of pointer can be passed into memcpy and memset, which truly reflects the significance of the memory operation function, because the object it operates on is only a piece of memory, regardless of the memory type (see the C language for generic programming ). If the parameter types of memcpy and memset are not void * But char *, it is really strange! Such memcpy and memset are obviously not "pure, out of low-level interests" functions! The emergence of void is only for an abstract need. If you have understood the concept of "abstract base class" in object-oriented, it is easy to understand the void data type. Just as we cannot define an instance for an abstract base class, we cannot define a void (let's say that void is an abstract data type) variable.

 

Related Article

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.