char *foo = "000"; *(foo+0) = '1'; *(foo+1) = (char)'2'; *(foo+2) = '3';
In this code, the "000" is part of the program and is protected against changes. If it was allowed, you would end up with a self modifying program.
char *foo = malloc(4); *(foo+0) = '1';
*(foo+1) = '2';
*(foo+2) = '3';
*(foo+3) = '\0';
Here , you allocate some memory in a space that allow changes, it is where your variables data is stored.
Quote:
This issue can be solve by using malloc(), but isn't the previous code dealing with "Stack", and this one dealing with "Heap"?
In both codes, the pointer is on stack, but in first, it is pointing to exe code and is not changeable, in second data is in heap.