对于这个问题,不少人确定想固然的说必须强制转换之类的云云……实则否则,今天看《C语言核心技术》的时候明确说到html
能够将一个void指针值指定给另外一个对象指针数据类型;反之也行,这都不须要进行显式数据类型转换。
而后在c语言邮件组常见问题里发现这个了,原文是http://c-faq.com/malloc/mallocnocast.html,内容以下:测试
Q: What's wrong with casting malloc's return value?spa
A: Suppose that you call malloc but forget to #include <stdlib.h>. The compiler is likely to assume that malloc is a function returning int, which is of course incorrect, and will lead to trouble. Now, if your call to malloc is of the formprototype
char *p = malloc(10);
the compiler will notice that you're seemingly assigning an integer to a pointer, and will likely emit a warning of the form ``assignment of pointer from integer lacks a cast'' (see question 7.6), which will alert you to the problem. (The problem is of course that you forgot to #include <stdlib.h>, not that you forgot to use a cast.) If, on the other hand, your call to malloc includes a cast:指针
char *p = (char *)malloc(10);
the compiler is likely to assume that you know what you're doing, that you really do want to convert the int returned by malloc to a pointer, and the compiler therefore probably won't warn you. But of course malloc does not return an int, so trying to convert the int that it doesn't return to a pointer is likely to lead to a different kind of trouble, which will be harder to track down.code
(Of course, compilers are increasingly likely--especially under C99--to emit warnings whenever functions are called without prototypes in scope, and such a warning would alert you to the lack of <stdlib.h> whether casts were used or not.)orm
总结一句话,C语言中的void返回值类型不须要进行强制类型转换。(C++好像要,否则会报错)htm
ps:对于C中不转换会不会出问题或者警告,我测试了,没问题,环境是MinGW对象