Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of void in C language

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the use of void in C language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

In English, void is interpreted as "void, space, gap" as a noun, while in C language, void is translated as "untyped", and the corresponding void * is "untyped pointer".

Void seems to have only the function of "comments" and limiting programs, of course, the "comments" here are not to provide comments for us, but to provide a so-called annotation for the compiler.

It is quite common that the function of void restricts the return of a function. The qualification of function parameters is also common.

Generally speaking, these two situations are common to us:

When a function does not need to return a value, it must be qualified with void, which is what we call the first case. For example: void func (int aMaginchar * b). When a function does not allow arguments, it must be qualified with void, which is what we call the second case. For example: int func (void). Rules for using void pointers

\ 1. Void pointers can point to any type of data, that is, any type of pointer can be used to assign values to void pointers to void pointers. For example:

Int * a _

If you want to assign void pointer p to other types of pointers, you need to cast, in this case: a = (int *) p. In the memory allocation, we can see the use of void pointers: the pointer returned by the malloc function of the memory allocation function is void *. When users use this pointer, they have to perform a forced type conversion, that is, explicitly indicating what type of data the pointer points to in memory (int) malloc (1024) means that the void pointer returned by malloc is required to store int data in memory one by one.

\ 2. In the ANSI C standard, it is not allowed to perform some arithmetic operations on void pointers, such as paired + or paired pointer 1, because since void is untyped, we do not know how many bytes of each arithmetic operation, such as char operation sizeof (char) bytes, while int operates on sizeof (int) bytes. It is allowed in GNU, because by default, GNU thinks that void * is the same as char *, and since it is certain, you can certainly do some arithmetic operations, where sizeof (* p) = = sizeof (char).

Void almost only "comments" and restricts the role of the program, because no one has ever defined a void variable, let's try to define:

Void a

This line of statement will cause an error when compiling, prompting "illegal use of type 'void'". Even if the compilation of void a doesn't go wrong, it doesn't make any practical sense.

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

Float * p1ntint * p2transp1 = p2dexapacer / where p1 = p2 statement will cause compilation error, / / prompt "'=': cannot convert from 'int *' to 'float *", which must be changed to: p1 = (float *) p2

Unlike void *, pointers of any type can be assigned directly to it without having to cast.

Void * p1umbint * p2umbint p1 = p2

However, this does not mean that void * can also be assigned to pointers of other types without casting. Because "untyped" can contain "typed", while "typed" cannot contain "untyped".

Be careful with the void pointer type:

According to the ANSI (American National Standards Institute) standard, void pointers cannot be algorithmically manipulated, that is, the following operations are illegal:

Void * pvoid;pvoid++; / / ANSI: error pvoid+ = 1; / / ANSI: error / / the ANSI standard determines this because it insists that the pointer to the algorithm must be sure that it points to the size of the data type. / / for example: int * pint;pint++; / / ANSI: correct

The result of pint++ is to increase its sizeof (int). However, GNU does not think so, specifying that the algorithm operation of void * is the same as that of char *. So the following statements are correct in the GNU compiler:

Pvoid++; / / GNU: correct pvoid+ = 1; / / GNU: correct

The result of the execution of pvoid++ is that it increases by 1.

In actual programming, in order to meet the ANSI standard and improve the portability of the program, we can write code that implements the same function:

Void * pvoid; ((char *) pvoid) + +; / / ANSI: error; 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 syntax. However, in real design, we should try our best to meet the ANSI standard. If the argument to a function can be a pointer of any type, its argument should be declared to be void *.

Note: void pointers can be any type of data, can bring us some benefits in the program, when the function is shaped as a pointer type, we can define it as a void pointer, so that the function can accept any type of pointer. Such as:

Typical function prototypes such as memory operation functions 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, pointers of any type can be passed into memcpy and memset, which truly reflects the meaning of memory manipulation functions, because the object it operates on is only a piece of memory, regardless of the type of memory (see C language for generic programming). It would be really weird if the parameter type of memcpy and memset is not void *, but char *! Such memcpy and memset are obviously not a "pure, out of taste" function! The emergence of void is only for an abstract need, if you correctly understand the concept of "abstract base class" in object-oriented, it is also easy to understand the void data type. Just as we cannot define an instance of an abstract base class, we cannot define a void (let's analogically call void an "abstract data type") variable.

Thank you for reading this article carefully. I hope the article "what is the use of void in C language" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report