Hi guys I am a beginner in C, and I have trouble when testing array pointer and allocating dynamic memory.
int x,i=0;
char *myChar;
printf("Please enter the length: ");
scanf("%d",&x);
myChar = (char*)malloc(x*sizeof(char));
printf("Please enter %d characters:",x);
for(i=0;i<x;i++){
scanf("%c ",&myChar[i]);
}
for(i=0;i<x;i++){
printf("%c",myChar[i]);
}
free(myChar);
If I enter 5 for length, and "hello" for character array, I get "hell" instead of "hello", which is not fun. Anybody can help me with this? Thanks