You can write to memory addresses which are owned by that particular entity.
For eg:
char s[10];
The compiler reserves enough memory required to store 10
characters for s
and you can freely write to it.
When you say:
char *s;
The pointer s
just points to some random memory address which is not owned or reserved for it. Writing to that memory address results in writing to memory owned by some other entity. Technically, this is Undefined Behavior.
Practically, a segfault might occur or not depending on the memory address being written to is owned by some other entity. So you are lucky to get a crash which draws your attention to it. Anyhow, It is undefined behavior and hence should always be avoided.
You need to allocate memory to the pointer to be able to anything meaningful with it.Be it memory on stack or heap, but it should be owned and hence allwed to write to.