With strings, you should not assign your values ββwith the assignment operator ( = ). This is because strings are not the actual type, they are just char pointers. You should use strcpy instead.
The problem with your code is that you allocated memory for foo , then reassign foo to another memory address, which is READONLY. When you assign foo[0] , you get a runtime error because you are trying to write to read-only memory.
Correct your code by following these steps:
char* foo = malloc(50); strcpy(foo, "testing");
This works because foo points to the address you allocated.
source share