Skip to content

Instantly share code, notes, and snippets.

@theclanks
Created February 8, 2013 03:16
Show Gist options
  • Save theclanks/4736337 to your computer and use it in GitHub Desktop.
Save theclanks/4736337 to your computer and use it in GitHub Desktop.
exemplo simples realloc
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(){
char *buffer = NULL;
char palavra;
printf("Alocando memoria\n");
buffer = (char *) malloc(1 + sizeof(char));
while (1){
printf("Digite a string para concatena-la: \n");
scanf("%s", &palavra);
if (strcmp(&palavra, "exit") == 0)
break;
/* Alocacao dinamica */
buffer = (char *)realloc(buffer, strlen(buffer) + strlen(&palavra) + sizeof(char));
printf("Memoria alocada\n");
if (!buffer) {
printf("Deu pau");
free(buffer);
return 0;
}
/* Concatenando a parada */
strcat(buffer, &palavra);
printf("String concatenada %s\n", buffer);
}
free(buffer);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment