1. void
The void keyword represents the concept of "empty type". However, the "empty type" here does not mean "any type", but means that it does not exist. That is to say, C/C++ does not allow you to write the statement void a, and there is no thing of type void.
void means " The meaning of "does not exist" can be reflected from the two applications of void:
1. When void is used as the return value type of a function, it means that the function return value does not exist, that is, the function has no return value.
For example:
void FunctionNoReturn_0(std::string strName)
{
std::cout << strName << std::endl;
return;
}
void FunctionNoReturn_1(std::string strName)
{
std::cout << strName << std::endl;
}
FunctionNoReturn_1 Although there is no explicit Return; statement in the function body. However, there is an implicit Return; indicating that the function does not have a return value.
FunctionNoReturnType(void)
{
return 10;
}
In C language, any function that does not specify a return value type will be treated by the compiler as returning an integer value instead of having no return value. Therefore, it is correct that the FunctionNoReturnType function returns 10.
In C++, each function must limit the return value type, and no return value limit is allowed. Therefore, the C++ compiler will report an error to FunctionNoReturnType.
2. When void is used as a parameter limit for a function, it means that the function parameter does not exist, that is, the function has no formal parameter.
For example:
void FunctionNoArgument_0(void)
{
return;
}
void FunctionNoArgument_1()
{
return;
}
Note: void FunctionNoArgument_1(); This also means there are no formal parameters.
In C language, FunctionNoArgument_1(10); is legal. The compiler will not report an error.
In C language, FunctionNoArgument_0(10); is illegal. The compiler will report an error.
In C++ language, FunctionNoArgument_1(10); and FunctionNoArgument_0(10);
are both illegal. The compiler will report an error.
It doesn’t matter if there is no error in C language. Because parameter 10 has no effect on the result of function execution. However, code maintenance may cause hidden harm and may cause misunderstandings to others.
Note: Since these two uses of void are provided, use them. That is, if a function does not return a value, its return value type is written as void, and if a function has no formal parameters, its formal parameters are written as void. Don't rely on the compiler's default behavior if you don't understand it. Even if you understand its default operation, don't rely on it, because there will definitely be someone who doesn't understand it, and others will not be able to understand your code.
2. void*
void* means "null type pointer", which is different from void. void* means "pointer of any type" or means "the pointer is related to an address value, but the object at this address is not clear type". (Why not use void to represent any type of data? As we all know, C/C++ is a statically typed language. Defining a variable will allocate memory. However, different types of variables occupy different memory. If you define a variable of any type, How to allocate memory for it? Therefore, there are no variables of any type in C and C++. However, all pointer type variables, whether they are int*, char*, string*, Student*, etc., have the same memory space. , so "pointers of any type" can be defined).
C++/ANSI C:
void* pointers only support a few limited operations: comparing with another pointer; passing a void pointer to or returning a void* pointer from a function; assigning a value to another void* pointer. A void* pointer is not allowed to be used to manipulate the object it points to, for example, a void* pointer is not allowed to be dereferenced. Arithmetic operations on void* pointers are not allowed.
GNU C:
GNU C specifies that arithmetic operations on void* pointers are consistent with char*.
void* means "pointer of any type", which is mainly used in the formal parameter types and return value types of memory operation functions (memory operations have nothing to do with the data type in the memory, that is, any type is acceptable).
memcpy
Prototype: extern void *memcpy(void *dest, void *src, unsigned int count);
Usage: #include
Function: Copy count bytes from the memory area pointed to by src to the memory area pointed by dest.
Note: The memory areas pointed by src and dest cannot overlap. The function returns a pointer to dest.
Note: Compared with strcpy, memcpy does not encounter '