Created
February 8, 2013 03:16
-
-
Save theclanks/4736337 to your computer and use it in GitHub Desktop.
exemplo simples realloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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