void指针有什么用? [英] what is the use of void pointer?
问题描述
我不知道有什么用(例如void * pt),何时使用它。
谢谢!
i do not know what is the use of (e.g. void *pt), when will use it.
thanks!
推荐答案
当你不知道
包含的内存是什么数据类型时,你应该使用它。优点是,一旦你知道了数据类型,你可能会将void ptr转换为适当的数据类型。
You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
Viv写道:
当你不知道内存包含什么数据类型时,你应该使用它。优点是,一旦你知道数据类型,你可以将void ptr类型化为适当的数据类型。
You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
一个很好的例子就是的malloc"功能。它会返回一个void *,你可以将它变成你想要的任何类型。
原型:void * malloc(size_t size);
用法:int * n =(int *)malloc(sizeof(int));
A good example is the "malloc" function. It returns a void* and you can
typecast it to whatever type you need.
Prototype : void *malloc(size_t size);
Usage : int *n = (int *) malloc(sizeof(int));
Sandeep写道:
Sandeep wrote:
Viv写道:
Viv wrote:
当你不知道内存包含什么数据类型时,你应该使用它。优点是,一旦你知道数据类型,你可以将void ptr类型化为适当的数据类型。
一个很好的例子是malloc。函数。
它返回一个void *你可以将它转换为你需要的任何类型。
You should use it when you do not know what data type the memory
contains. The advantage is that once u know the data type, u may
typecast the void ptr into the appropriate data type.
A good example is the "malloc" function.
It returns a void* and you can
typecast it to whatever type you need.
" Typecasting"这是发生在thespians上的事情。
http://dictionary.reference.com/search?q=typecast&db=*
原型:void * malloc(size_t size);
用法:int * n =(int *)malloc(sizeof(int));
"Typecasting" is something that happens to thespians.
http://dictionary.reference.com/search?q=typecast&db=*
Prototype : void *malloc(size_t size);
Usage : int *n = (int *) malloc(sizeof(int));
一个更好的方法,没有演员。
int * n = malloc(sizeof * n);
-
pete
A more better way, is without the cast.
int *n = malloc(sizeof *n);
--
pete
这篇关于void指针有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!