1/* malloc example: random string generator*/
2#include <stdio.h> /* printf, scanf, NULL */
3#include <stdlib.h> /* malloc, free, rand */
4
5int main ()
6{
7 int i,n;
8 char * buffer;
9
10 printf ("How long do you want the string? ");
11 scanf ("%d", &i);
12
13 buffer = (char*) malloc (i+1);
14 if (buffer==NULL) exit (1);
15
16 for (n=0; n<i; n++)
17 buffer[n]=rand()%26+'a';
18 buffer[i]='\0';
19
20 printf ("Random string: %s\n",buffer);
21 free (buffer);
22
23 return 0;
24}