I have wrote two programs like this, I am inputting 'hello'
prog1:
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[10];
scanf("%s", arr);
printf("%s\n", arr);
return 0;
}
output : hello
prog2
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str;
scanf("%s", str);
printf("%s\n", str);
return 0;
}
output:segmentation fault
Based on my understanding that array name at run time will be changed to char * type. I thought str is already char * so it should be able to point a string in the second case.
1) How in this case array works but str does not?
2) Where the difference between arr and str starts in this program? Why it should be obvious to programmers?