I have some very basic questions regarding pointers and memory allocation.
In below code where does pointer
c
actually point to? In other words where does the string"xyz"
get stored in memory (stack/heap etc.)?What will happen to memory location allocated for
a
as I am not using it anymore?Code seems to work well if I un-comment the commented section. What's happening with the memory in that scenario?
#include <stdio.h> main() { char *c; //c = (char *)malloc(2); c = "a"; c = "xyz" ; printf("%s",c); return 0; }
Output:
xyz
Edit:
After reading few of the answer and first comment another question came up in my mind:
In below case, where do the strings get stored? Can I alter them later on?
char *c[] = {"a","xyz"};