malloc()函数:
语法: #include<stdlib.h>
void *malloc(size_t size);
malloc()返回一个地址(指针),该函数的返回类型为void指针。可以与所有的数据类型兼容(需要加强制类型转换)。
范例1:
#include <stdlib.h>
#include<stdio.h>
int main(void)
{
char *str;
str=(char *)malloc(100);
if(str==NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1);
}
printf("String was allocated !\n");
return 0;
}
范例2:
int *numbers;
numbers=(int*)malloc(50*sizeof(int));