当前位置:网站首页>Void* pointer

Void* pointer

2022-06-13 08:54:00 Human high quality Algorithm Engineer

You can see in the code (void )stDst.pucBuf The pointer , void What is the pointer for , See below ,

void Literally, it means “ No type ”,void * Then for “ No type pointer ”,*void Can point to any type of data .( The key )

Any type of pointer can be assigned directly to void*, There is no need to cast

void *p1;
int *p2;
p1 = p2;
void *arg;
int i;
i=int *)arg;

If you define void type :

void a;
This line will compile with an error , Even if void a There will be no errors in the compilation of , It doesn't have any practical significance either .

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 Variable .

void The rules of using keywords :

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

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

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

          4. void Can't represent a real variable ;

Here's the explanation :

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

(2) Restrictions on function 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 . for example :

add ( int a, int b )
{
    
return a + b;
} // The return is int type 

Of course we're writing C/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 class
type . This is the need for good readability of the program , It is also the requirement of programming standardization .

2. Careful use void Pointer types
   according to ANSI(American National Standards Institute) standard , Not right void Pointer for algorithm operation , The following operations are illegal :

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 . 
// for example :
int *pint;
pint++; //ANSI: correct 
原网站

版权声明
本文为[Human high quality Algorithm Engineer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270536289025.html