void指针(void pointer)

the meaning of void
void is "no type", void is "no type pointer", you can point to any data type

void pointer usage specification
1.void pointer can point to any data type, that is, any data type of pointer can assign to void pointer;
E.G.:
int *pint;
pvoid = pint; //but not pint = pvoid
if you want to assign pvoids to other types of pointer, you need to force type conversions such as:pint = (int*)pvoid

2.in the ANSA C standard,arthmetic operations on pvoid such as pvoid++ or pvoid+=1 are not allowed, but it is legal while in GNU, because GNU thinks void* is the same as char* by default. sizeof(*pvoid) = sizeof(char)

the role of void
2.1 limit the return of the function
2.2 limit the function paremeters
when the function does not need to return a value, you must use void to limit the return value type. for example: void func(int, int)
when the function is not allowed to accept parameters, you must use the void limit,for example: int func(void)

since void pointer can point to any type data, you can assign a void pointer to a any data type pointer,so you can also use the void pointer as a function paremeter, so that the function can accept any data type pointer as a parameter,
E.g:
void* memcpy(void*dest, const void* src, size_t len);

void的含义
void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型。

void指针使用规范
①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值。例如:
int *pint;
void *pvoid;
pvoid = pint; /* 不过不能 pint = pvoid; */
如果要将pvoid赋给其他类型指针,则需要强制类型转换如:pint = (int *)pvoid;


②在ANSI C标准中,不允许对void指针进行算术运算如pvoid++或pvoid+=1等,而在GNU中则允许,因为在缺省情况下,GNU认为void *与char *一样。sizeof( *pvoid )== sizeof( char ).

void的作用
①对函数返回的限定。
②对函数参数的限定。
当函数不需要返回值时,必须使用void限定。例如: void func(int, int);
当函数不允许接受参数时,必须使用void限定。例如: int func(void)。


由于void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值,因此还可以用void指针来作为函数形参,这样函数就可以接受任意数据类型的指针作为参数。例如:
void * memcpy( void *dest, const void *src, size_t len );
void * memset( void * buffer, int c, size_t num );

posted @ 2017-10-15 20:49  diamondDemand  阅读(1198)  评论(0编辑  收藏  举报