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

How to make a deep analysis of void types

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to carry out in-depth analysis of void types, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

The following will give an in-depth description of the void keyword, and describe in detail the methods and techniques for using void and void pointer types. Beginners do not quite understand the void and void pointer types in CAccord + language, so there are some errors in use.

The meaning of 1.void

Void literally means "untyped", void type * means "untyped pointer", and void * can point to any type of data. 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:

This line of statement will cause an error when compiling, prompting "illegal use of type 'void'". However, even if the compilation of void a does not go wrong, it does not make any practical sense. The real role of void is:

(1) restrictions on the return of a function

(2) the limitation of function parameters.

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.

It is illegal for int function (void) {return 1;} to make the following call: function (2); because in C++, the function argument to void means that the function takes no arguments. We compiled in Turbo C 2.0: # include "stdio.h" fun () {return 1;} main () {printf ("% d", fun (2)); getchar ();}

Therefore, in order to avoid confusion, we must specify the type of any function one by one when we write the Chammer Cleavage + program. If the function does not return a value, be sure to declare it as a void type. This is not only the need of good readability of the program, but also the requirement of programming standardization.

In addition, after adding the void type declaration, you can also play the role of "self-annotating" the code. The "self-comment" of the code means that the code can comment itself. Rule 2 if a function has no arguments, it should be declared as void

Declare a function like this in the C++ language:

It is illegal for int function (void) {return 1;} to make the following call: function (2); because in C++, the function argument to void means that the function takes no arguments. We compiled in Turbo C 2.0: # include "stdio.h" fun () {return 1;} main () {printf ("% d", fun (2)); getchar ();}

The compilation is correct and the output 1, which means that in C, you can pass any type of argument to a function without arguments, but there will be an error in compiling the same code in the C++ compiler. In C++, you cannot pass any parameters to a function with no arguments, with the error message "'fun': function does not take 1 parameters". Therefore, whether in C or C++, if the function does not accept any parameters, be sure to specify that the argument is void.

After reading the above, do you have any further understanding of how to conduct in-depth analysis of void types? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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