I expected that after
*b = a[0]
line:
printf("%c\n", *b);
would print first character from string a. Here is my short code:
#include <stdio.h>
int main(){
char a[20];
char* b;
scanf("%s", a);
printf("a: %s\n", a);
*b=a[0]; // I suppose something here is wrong
printf("%c\n",*b);
return 0;}
For input:
STRING
Output is:
a: STRING
Segmentation fault (core dumped)
As far as I know, segmentation fault occurs when we try to access to memory which isn't allocated (staticaly or dinamicaly). Here, b is pointer to char and it takes 8 bytes since my OS is 64-bit. Using *b we dereference pointer, i.e access to it's content. b has type char*, a[0] has type char and *b has type char. What's wrong?