I try to learn malloc function and I have some problems. This program compiles nicely but it crashes after I have entered one line text and pressed enter. Array is the array where I try to allocate all the text from input string.
On this context array = (char*) malloc(sizeof(char)*10); means that I have made an array named array with 10 cells: array[0], ..., array[9].
What's wrong and how to make it work?
Code:
int main(int argc, char* argv[])
{
char * input;
char * array;
input = (char*) malloc(sizeof(char)*1);
array = (char*) malloc(sizeof(char)*10);
int i=0;
printf("Please input some text\n");
while(*input!='0')
{
gets(input);
*(array+i)=*input;
if(*input!='0')
printf("You inserted, %d: %s\n",i+1, *(array+i));
i++;
}
return 0;
}