到底是不是MSDN错了?

zhoulch 2000-12-17 10:37:00
请看下面一段MCSN中的例子:/* MALLOC.C: This program allocates memory with
* malloc, then frees the memory with free.
*/

#include <stdlib.h> /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>

void main( void )
{
char *string;

/* Allocate space for a path name */
string = malloc( _MAX_PATH );
if( string == NULL )
printf( "Insufficient memory available\n" );
else
{
printf( "Memory space allocated for path name\n" );
free( string );
printf( "Memory freed\n" );
}
}
以上一段代码在VC6.0中无法编译通过,应该为:
/* MALLOC.C: This program allocates memory with
* malloc, then frees the memory with free.
*/

#include <stdlib.h> /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>

void main( void )
{
char *string;

/* Allocate space for a path name */
string =(char*) malloc( _MAX_PATH );
if( string == NULL )
printf( "Insufficient memory available\n" );
else
{
printf( "Memory space allocated for path name\n" );
free( string );
printf( "Memory freed\n" );
}
}
只有这样才对。
到底是不是MSDN错了?
...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
789 2000-12-18
  • 打赏
  • 举报
回复
第一个程序编写很不规范,是一个不好的习惯,ed9er的解释很对。

malloc()函数原形说明中就有(datatype *)malloc(....
pccrazy 2000-12-18
  • 打赏
  • 举报
回复
应该算你的错,在VC中void* 与 char*是不同的数据类型,要用强行转换
comanche 2000-12-18
  • 打赏
  • 举报
回复
算是个错吧,但程序员都不会放这个错
因为 malloc分配后的返回值是个 void *
早期C不会报错
当然好的习惯还是要加个 ( char * )
ed9er 2000-12-18
  • 打赏
  • 举报
回复
你用cc编译器就不会报错了,malloc是ANSI规范里的,而ANSI里的void可以赋给任何类型的
揪这些鸡毛蒜皮有什么意义?




zhoulch 2000-12-18
  • 打赏
  • 举报
回复
不是我没有强性转换,是msdn没有!

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧