Deep Exploration of void type

Source: Internet
Author: User

This article provides a deep description of the void keyword and describes how to use the void and void pointer types. Beginners do not understand the void and void pointer types in C/C ++, so there are some errors...

1. Meaning of void
Void literally means "no type", void type * is "no type Pointer", void * can point to any type of data. 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:

An error occurs when compiling this line of statements, and the message "illegal use of type 'void'" is displayed '". However, even if the compilation of void a does not go wrong, it has no practical significance. Void actually plays the following role:
1) limits on function return;
2) limits function parameters.
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.

 
 
  1. Int function (void)
  2. {
  3. Return 1;
  4. }
  5. The following call is invalid:
  6. Function (2 );
  7. In C ++, the function parameter void means that this function does not accept any parameters.
  8. We compile in Turbo C 2.0:
  9. # Include "stdio. h"
  10. Fun ()
  11. {
  12. Return 1;
  13. }
  14. Main ()
  15. {
  16. Printf ("% d", fun (2 ));
  17. Getchar ();
  18. }

Therefore, in order to avoid confusion, when writing C/C ++ programs, you must specify the type of any function. If the function does not return a value, it must be declared as void. This is both a need for good program readability and a requirement for standardized programming.

In addition, after adding the void type declaration, you can also use the "self-annotation" function of the Code. The "self-annotation" of the code means that the code can annotate itself. Rule 2 If the function has no parameters, the parameter should be declared as void
Declare a function in C ++:

 
 
  1. Int function (void)
  2. {
  3. Return 1;
  4. }
  5. The following call is invalid:
  6. Function (2 );
  7. In C ++, the function parameter void means that this function does not accept any parameters.
  8. We compile in Turbo C 2.0:
  9. # Include "stdio. h"
  10. Fun ()
  11. {
  12. Return 1;
  13. }
  14. Main ()
  15. {
  16. Printf ("% d", fun (2 ));
  17. Getchar ();
  18. }

Compiling is correct and 1 is output. In C, parameters of any type can be transferred to a function without parameters. However, compiling the same code in C ++ compiler will cause an error. In C ++, no parameters can be transferred to a function without parameters. If an error occurs, the system prompts "'fun ': function does not take 1 parameters ". Therefore, whether in C or C ++, if the function does not accept any parameters, you must specify the parameter as void.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.