/* 使用malloc函数从堆上分配内存 */ |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
int main( void ) { |
int * p = NULL; |
/* |
*p = 2; 此时对*p赋值为错误的。 |
*/ |
p = ( int *) malloc ( sizeof ( int )); |
if (NULL == p) { |
printf ( "Can’t get memory!\n" ); |
return -1; |
} |
printf ( "%d\n" , *p); |
memset (p, 0, sizeof ( int )); |
printf ( "%d\n" , *p); |
*p = 2; |
printf ( "%d\n" , *p); |
return 0; |
} |
by: 发表于:2017-08-21 14:33:56 顶(0) | 踩(0) 回复
??
回复评论