当前位置:网站首页>Void keyword

Void keyword

2022-07-06 22:31:00 It's Beichen not too PI acridine

void Literally, it means “ Empty type ”,void * Then for “ Null type pointer ”,void * Can point to any type of data .
void Almost only “ notes ” And limiting the role of procedures , Because no one ever defines a void Variable .

void The real role is :
(1) A restriction on the return of a function ;
(2) Restrictions on function parameters .

as everyone knows , If the pointer p1 and p2 Same type of , Then we can directly p1 and p2 They assign values to each other ; If p1 and p2 Point to different data types , You must use the cast operator to convert the type of the pointer on the right side of the assignment operator to the type of the pointer on the left .

float *p1;
int	*p2;
p1 = (float *)p2;

and void * Is different , Any type of pointer can be assigned directly to it , There is no need to cast :

void *p1;
int	*p2;
p1 = p2;

But that doesn't mean ,void * You can also assign pointers to other types without having to cast them . because “ Empty type ” can
With inclusiveness “ There are types ”, and “ There are types ” We can't tolerate “ Empty type ”.
The following statement compilation error :

void *p1;
int	*p2;
p2 = p1;

void Modify function return values and parameters

1. If the function does not return a value , Then it should be declared as void type .

stay C In language , Any function without return value type restriction , Will be handled by the compiler as a return integer value . But many programmers mistake it for void type .

add ( int a, int b )
{
    
	return	a + b;
}

int main(int argc, char* argv[]) 
{
    
	printf ( "2 + 3 = %d", add ( 2, 3) );
}

The result of program running is output : 2 + 3 = 5
This shows that the function without return value description is indeed int function .

therefore , To avoid chaos , We're writing C The program , For any function, its type must be specified one by one . If the function does not return a value , Be sure to state that void type . This is the need for good readability of the program , It is also the requirement of programming standardization . in addition , add void After type declaration , Can also play the role of code “ Self annotation ” effect . The so-called code “ Self annotation ” That is, the code can annotate itself .

2. If the function has no arguments , Then it should be declared that the parameter is void

int function(void)
{
    
	return 1;
}

It is illegal to make the following call :function(2);
The parameter of the function is void This function takes no arguments .

#include "stdio.h"

fun()
{
    
	return 1;
}

int main()
{
    
	printf("%d",fun(2));
	getchar(); 
}

Compile correctly and output 1, This explanation , stay C In language , You can pass any type of parameter to a function without parameters .

void The pointer

1. Be careful and use carefully void Pointer types .
according to ANSI(American National Standards Institute) standard , Not right void Pointer for algorithm operation .

void * pvoid;
pvoid++; //ANSI: error 
pvoid += 1; //ANSI: error 

ANSI The reason why the standard is so recognized , It's because it insists on : The pointer for algorithm operation must be determined to know the size of the data type it points to . In other words, you must know the exact value of the memory destination address .

int *pint;
pint++; //ANSI: correct 

But famous GNU(GNU’s Not Unix The recursive abbreviation of ) I don't think so , It specifies void * The algorithm operation and char * Agreement . So the following statement is in GNU It's all right in the compiler :

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

In actual programming , To conform to ANSI standard , And improve the portability of the program , We can write
Code that performs the same function :

void * pvoid;
(char *)pvoid++; //ANSI: correct ;GNU: correct 
(char *)pvoid += 1; //ANSI: error ;GNU: correct 

GNU and ANSI There are also some differences , Overall speaking ,GNU a ANSI more “ to open up ”, Provides support for more Syntax . But when we're actually designing , We should try our best to conform to ANSI standard .

If the argument to a function can be a pointer of any type , Then it should be declared that the parameter is void *.

Typical examples are memory operation functions memcpy and memset The function prototypes of are :

void * memcpy(void *dest, const void *src, size_t len);
void * memset ( void *buffer, int c, size_t num );

such , Any type of pointer can be passed in memcpy and memset in , This also truly reflects the meaning of memory operation function , Because the object it operates on is just a piece of memory , No matter what type of memory it is .

memset Accept any type of pointer

int IntArray_a[100];
memset (IntArray_a, 0, 100*sizeof(int) ); // take  IntArray_a  clear  0

memcpy Accept any type of pointer

int destIntArray_a[100], srcintarray_a[100]; 
// take  srcintarray_a  Copy to  destIntArray_a
memcpy (destIntArray_a, srcintarray_a, 100*sizeof(int) );

memcpy and memset Function also returns void * type

void Can't represent a real variable

Because memory space must be allocated when defining variables , Definition void Type variable , How much memory does the compiler allocate .
The following code attempts to make void Represents a real variable , So it's all wrong code :

void a; // error 
function(void a); // error 

void It's just an abstract need , If you understand object-oriented correctly “ Abstract base class ” The concept of , And it's easy to understand void data type . Just as you can't define an instance of an abstract base class , We can't define a void( Let's call it by analogy void by “ Abstract data types ”) Variable .

原网站

版权声明
本文为[It's Beichen not too PI acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061439469436.html