You do not have any memory allocated for s
. It's just a pointer that is either NULL or pointing to a random location (depending on the compiler). Trying to write to it is like going to bed without caring where you are - and the probability that you've just broken into someone else's house is way larger than randomly selecting your own bed. Thus, Segmentation Fault - the computer's way of getting your program arrested.
To allocate the memory, you can either let the compiler set some aside for you (char s[22]
), or allocate your own (char *s = malloc(22)
).