1.calloc (Configure memory space)
Related functions:
Malloc,free,realloc,brk
Table header file:
#include <stdlib.h>
To define a function:
void *calloc (size_t nmemb,size_t size);
Function Description:
Calloc () is used to configure the NMEMB contiguous unit of memory, each unit of size, and returns a pointer to the first element. This works the same way as in the following ways: malloc (nmemb*size), but the memory content is initialized to 0 when the calloc () is used to configure memory.
return value:
If the configuration succeeds, a pointer is returned and NULL is returned if it fails.
Example one:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i;
int *pn= (int *) calloc (10,sizeof (int));
for (i=0;i<10;i++)
printf ("%3d", *pn++);
printf ("\ n");
Free (PN);
return 0;
}
Case TWO:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char *str = NULL;
/* Allocate memory space/
char *str = (char*) calloc (sizeof (char));
/* Write Hello to * *
strcpy (str, "Hello");
http://www.bianceng.cn
/* Display variable content
/printf ("String is%s\n", str);
/* Free Space * *
(str);
return 0;
}