/* 使用malloc函数从堆上分配内存 */ #include #include #include 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; }