laitimes

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

author:Little Cute Nine Tails

Yesterday we talked about enumeration types in data types, and today we talk about the void type in data types.

When you see the word "void", do you feel very familiar with it, yes, this is a keyword, we usually use it to define functions and methods.

But I'm ashamed to say that I have been learning C for so long, until now, I have not fully understood the meaning of void, usually only know how to use void to define a method and function.

While reviewing the C supplement, I officially noticed the concept of void.

Void types, also known as no data types, in other words, have no data types.

So since void is defined without data type, it can also be used to define data as the "enumeration type" and "basic type" said two days ago.

But I tried to define a variable using void and found that it was reported incorrectly.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

Therefore, to define a variable, you still have to give the specified data type.

So, where is the void type mainly used?

According to the relevant information consulted on the Internet, void is mainly used to limit the return of functions and the restriction of parameter returns.

This sentence is more difficult to understand, and when I saw this sentence, I was also a little confused.

But after combing, it is easier to understand.

That is, there are three main cases of the void type, namely "function return is empty", "function argument is empty", "pointer points to void", but these three cases, so far I can only explain the first two, for the third case is still a little unclear, perhaps need to make a follow-up supplement.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

We can use void to define the function, but for the main function main(){} it will be found that using void main(){} will produce a warning, because the return value of the main function is of the int integer type, so to use int main(){} as the main method, the main difference between the two is whether there is a return value or not.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

Obviously, there are two cases where the function argument is empty, one is to add nothing inside the parentheses, and the other is to add void.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

malloc is a memory allocation function, because the pointer points to void I do not understand very well, so here is an example of a memory allocation function that is commonly used by everyone, and when I study it again, I may understand it almost.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)
Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

But obviously, there are still some problems, but this is based on online statements, which is obviously problematic.

So I modified the code based on my own understanding, that is, according to the definition of the pointer, to use the symbol & to access the memory address.

It can be found that this is correct, and so far I have found that the most effective role of void is that there is no need to force type conversion, which can help with simple procedures.

Discuss "data void types" in C, and programming supplementary learning begins (Section 5)

In general, the void type is actually less we usually use, so there is not much concept of this type, today is mainly to mention it, and then if you encounter it again, I will write the corresponding article according to the corresponding topic to explain.

Read on