is it necessary to type
Possible Duplicate:
Do I cast the result of malloc?
I was googling to find out the reason for type-casting of malloc and calloc . But, i only found type-casting of malloc is not necessary since it return void pointer but, what about calloc . This is the same reason for calloc too ???
Now, if we move back to first point, about return value of malloc and calloc . Then, i found that, both are returning the allocated spaces. So, i'm little bit confused here. So, my questions are
What is the return value of malloc and calloc
Is it necessary to type-cast malloc and calloc . And why ?
What is the return value of malloc and calloc?
It is a pointer to void
( void*
).
Is it necessary to type-cast malloc and calloc. And why ?
No, because the conversion from a pointer to void
to a pointer to object is implicit.
C11 (n1570), § 6.3.2.3 Pointers
A pointer to void
may be converted to or from a pointer to any object type.
It is right for both malloc
and calloc
.
malloc()
或calloc()
返回void *
,它可以分配给任何指针类型。在C中,不需要对void*
进行类型转换,因为它是由编译器隐式地完成的。但是在c ++中,如果你不会强制转换
The return value of malloc
and calloc
is a void*
. The address of the heap memory space allocated by these functions.
Both functions allocate memory. The only difference between them is that
In C its not recommended to cast the return value of these functions.
In C++ it is mandatory to cast.
链接地址: http://www.djcxy.com/p/28360.html上一篇: 如何将字符串传递给C中的char数组
下一篇: 是否需要输入