#include <stdlib.h>
void *calloc( size_t num, size_t size );
函数返回一个指向 num 数组空间,每一数组元素的大小为 size。如果发生错误,则返回 NULL。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *str = NULL;
/* allocate memory for string */
str = calloc(10, sizeof(char));
/* copy "Hello" into string */
strcpy(str, "Hello");
/* display string */
printf("String is %s\n", str);
/* free memory */
free(str);
return 0;
}输出结果:
String is Hello