Allocate memory for a 100-character string - C Memory

C examples for Memory:malloc

Description

Allocate memory for a 100-character string

Demo Code

#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;
}

Result


Related Tutorials