malloc, descripcion y ejemplo

Dependencies:   mbed

main.cpp

Committer:
sherckuith
Date:
2012-04-03
Revision:
0:984f552374ff

File content as of revision 0:984f552374ff:

#include "mbed.h"
/* malloc example: string generator*/
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}