C examples for Memory:malloc
Allocate memory for a 100-character string
#include <stdlib.h> #include <stdio.h> int main( void ) { /* allocate memory for a 100-character string */ char *str;//w ww . j a v a2 s . co m str = (char *) malloc(100); if (str == NULL){ printf( "Not enough memory to allocate buffer\n"); exit(1); } printf( "String was allocated!\n" ); free(str); return 0; }