Angel David Yaguana Hernandez / Mbed 2 deprecated CPP_malloc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /* malloc example: string generator*/
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 int main ()
00007 {
00008   int i,n;
00009   char * buffer;
00010 
00011   printf ("How long do you want the string? ");
00012   scanf ("%d", &i);
00013 
00014   buffer = (char*) malloc (i+1);
00015   if (buffer==NULL) exit (1);
00016 
00017   for (n=0; n<i; n++)
00018     buffer[n]=rand()%26+'a';
00019   buffer[i]='\0';
00020 
00021   printf ("Random string: %s\n",buffer);
00022   free (buffer);
00023 
00024   return 0;
00025 }