0 votes 0 votes This code executes successfully int main() { char str[20]; gets(str); puts(str); return 0; } whereas the below code gives runtime error int main() { char *str; gets(str); puts(str); return 0; } why so? Kiran Karwa asked Apr 7, 2018 Kiran Karwa 427 views answer comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments Kiran Karwa commented Apr 7, 2018 reply Follow Share @pankaj_vir even scanf needs a size? so does that mean we can never get a string from input console if its declared as char* ? 0 votes 0 votes pankaj_vir commented Apr 7, 2018 reply Follow Share @Kiran Karwa, we need the size of string for the input.if its declared as char* then we need to use dynamic memory. 1 votes 1 votes Devshree Dubey commented Apr 7, 2018 reply Follow Share @Kiran Karwa, #include<stdio.h> #include<stdlib.h> int main() { char str[20]; char *ptr; int n; //case 1 printf("Input a string"); gets(str); puts(str); //case 2 printf("Enter the size of the string"); scanf("%d",&n); ptr=(char *)malloc(n*sizeof(char)); ptr="Hope is the bird with Feathers"; printf("%s",ptr); free(ptr); return 0; } Please kindly execute this code snippet. I hope you'll understand the difference between character array and pointer. 1 votes 1 votes Please log in or register to add a comment.