malloc分配内存-----一个有错

来源:互联网 发布:淘宝号码没有信誉 编辑:程序博客网 时间:2024/06/06 09:22
#include <stdio.h>
#include <stdlib.h>

char * init(char *p)
{
p = malloc( sizeof(char)) ;
if( !p )
{
printf("p is NULL !\n");
return NULL ;
}
return p ;
}

char * init_test(char **p)
{
*p = malloc( sizeof(char)) ;
if( !*p )
{
printf("*p is NULL !\n");
return NULL ;
}
return *p ;
}



char * init_char()
{
char *p = malloc( sizeof(char)) ;
if( !p )
{
printf("p is NULL !\n");
return NULL ;
}
return p ;
}

int main()
{
char * test = NULL ;
test = init_char();
if( !test )
{
printf("p is NULL !\n");
}

char * xiejian = NULL;
init(xiejian);
if( !xiejian )
{
printf("xiejian is NULL !\n");
}

char *test2 = NULL ;
init_test( &test2 );
if( !test2 )
{
printf("test2 is NULL !\n");
}
return 0;
}
原创粉丝点击