simple use of malloc and realloc
Hi guys i want to improve my knowledge about the dyn allocation of char pointers... with this code i wanted to type a string and insert the string in a array created dynamically:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char c;
char *test=NULL;
unsigned int len;
test = (char*) malloc (sizeof(char));
printf("Gimme a string: ");
while(c != '\n'){
scanf("%c", &c);
test = realloc (test, sizeof(char));
len = strlen(test);
test[len]=c;
test[len+1]='\0';
}
printf("\nThis is the dynamic string: %s", test);
printf("\n\n");
system("PAUSE");
return 0;
}
That crew this output:
Quote:
Gimme a string: Hello World
This is the dynamic string: °):Hello World
and i don't understand why there are these 3 initial character '°' ')' ':' that i didn't have typed...