Mega Code Archive

 
Categories / C / Memory
 

Allocate memory block

#include <stdio.h> #include <stdlib.h> int main () {   int i, n;   char *str;   printf ("String Length? ");   scanf ("%d", &i);   str = (char*) malloc (i+1);   if (str == NULL)        exit (1);   for ( n = 0; n < i; n++)       str[n] = rand() % 26 + 'a';      str[i] = '\0';   printf ("Random string: %s\n", str);   free (str);   return 0; }