返回值
此函数返回指向分配的内存的指针,如果请求失败,则返回NULL。
示例
以下示例显示malloc函数的用法-
#include <stdio.h>
#include <stdlib.h>
int main () {
char *str;
/* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, "jc2182");
printf("String = %s, Address = %u\n", str, str);
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
free(str);
return(0);
}
让我们编译并运行上面的程序,它将产生以下结果-
String = jc2182, Address = 355090448
String = cainiaoya.com, Address = 355090448