Deep understanding of void

Author: The little sapling longs to become a towering tree
Author’s Declaration: Write every blog carefully
Author gitee: gitee

If you like the author’s article, give the author some attention!

void

  • foreword
  • 1. The void keyword
  • Two, void modified function return value and parameters
  • Three, void pointer
    • 3.1Can the pointer variable defined by void * be operated on?
    • 3.2void * is used to design a general interface
  • Four. Summary

Foreword

I believe that everyone’s subconscious understanding of void will explain the meaning of empty type. I think void does not have so many functions. This blog will let you fully understand the meaning and function of void.

1. void keyword

Generally speaking, void is at the same level as other data types, but the void keyword is used in many ways, and there are some things that it cannot do.

Can void define variables?

#include <stdio.h>
#include <windows.h>
int main()
{<!-- -->
void a;
system("pause");
return 0;
}

operation result:

We see that void cannot define variables. We know how much space is allocated for defining variables with data types, and the size of the type plays a role in opening up much space. Let’s take a look at the size of void:


Now we know why void can’t define variables, this is the result seen on vs2019, then let’s look at the result in the gcc compiler of vscode:

We see that the size of void on the gcc compiler is 1, why can’t variables be defined?

void itself is regarded as an empty type by the compiler, and variables are not allowed to be defined forcibly

Summary: Why can’t void define variables?
Define the essence of variables: open up space
As an empty type, void should not open up space in theory. Even if space is opened, it is only treated as a placeholder. Therefore, since the space cannot be opened, it cannot be used as a normal variable. Since it cannot be used, compile The device simply does not let him define the amount.

2. void modified function return value and parameters

Scenario 1: void is used as a function return value

test()
{<!-- -->
;
}
int main()
{<!-- -->
int a = test();
printf("%d", a);
system("pause");
return 0;
}


We see that it is correct that the function does not need to return a value, and the int type is returned by default. If you do not use void, the programmer will not know whether you have no return value or return an int type. What are the benefits of void? ? 1. Placeholder, let users know that no return value is needed. 2. Inform the compiler that the return value cannot be received

Scenario 2: void as a function parameter
Let’s look at a code first: function parameter passing without parameters

void test()//No parameters by default
{<!-- -->
;
}
int main()
{<!-- -->
test(2,5,"niaho");
system("pause");
return 0;
}

There will be no error in this code, because you pass it in, but nothing is received

void test(void)//clearly tell you not to use parameters
{<!-- -->
;
}
int main()
{<!-- -->
test(2,5,"niaho");
system("pause");
return 0;
}

In this way, an error will be reported. It is clearly stated that no parameters are required, and an error will be reported if you pass the parameters.

Conclusion:
1. If a function has no parameters, it is a good habit to set the parameter list to void, because errors can be clearly detected in advance
2. In addition, people who read your code can also see at a glance that no parameters are needed. Equivalent to “self-explanatory”

As an aside, though, if you’re not used to this, don’t force it.

3. void pointer

Void cannot define variables, so what about void*?

#include <stdio.h>
#include <windows.h>
int main()
{<!-- -->
void* p = NULL; // can
system("pause");
return 0;
}

We see that it can pass and there is no alarm, why can void? Because void is a pointer, it is a pointer, and the size of the space can be clarified

Let’s take a look at the features of void*:

#include <stdio.h>
#include <windows.h>
int main()
{<!-- -->
void *p = NULL;
int *x = NULL;
double *y = NULL;
p = x; //Although the types are different, the compiler does not report an error
p = y; //same as above
x=p;
y=p
system("pause");
return 0;
}

We see that the compiler leaves no error, we can draw the following conclusions:
1. The role of void is to accept any pointer type.
2. Any pointer can also accept void
type.

This piece is useful if you want to design a general interface later, for example:

/void * memset ( void * ptr, int value, size_t num );

3.1Can the pointer variables defined by void * be used for operations?

We introduced above that void can define pointer variables. We know that pointer variables can perform operations. Can void* pointers perform operations? Let’s take a look together:

(NULL is actually a strong conversion of 0 to void* type)

#include <stdio.h>
#include <windows.h>
int main()
{<!-- -->
void *p = NULL;
p + + ; //report an error
p + = 1; // report an error
system("pause");
return 0;
}


We said before in the pointer section that the size of pointer arithmetic is related to the type, for example, the type of int, adding 1 will skip one Integer type, then void does not know the size on vs2019, so it is not known to add 1 naturally, so an error will be reported.

Let’s take a look at the results on the gcc compiler

We see that the gcc compiler can pass, the reason is that the void size of the gcc compiler is 1, and it has a fixed size

3.2void * is used to design common interfaces

For a function, the type we need to accept may be different. At this time, we need to use void* as the interface to implement. Here I will not introduce the examples that require interfaces. You can see the examples of interfaces that I thanked before:
qsort
memory manipulation function

4. Summary

Through this blog, I hope that everyone has a deeper understanding of the meaning and usage of the void type. This article is not difficult, just to expand our knowledge. I hope to be a moderator to everyone. Today we will talk about here, see you next time