debugger-lab/literal.c
2026-10-20 23:15:48 -06:00

23 lines
784 B
C

/* The same letters in two different homes.
*
* The array holds its own cells, so you may write to them. The pointer holds an
* address, and the letters it points at are stored where writing is not allowed.
* Build with -DWRITE_THROUGH_LITERAL to watch the second case fail on purpose.
*/
#include <stdio.h>
int main(void) {
char copy[] = "quiet"; /* your own cells, filled from the letters */
char *literal = "quiet"; /* one pointer, aimed at read-only storage */
copy[0] = 'Q';
printf("copy: %s\n", copy);
#ifdef WRITE_THROUGH_LITERAL
literal[0] = 'Q'; /* undefined behaviour: there is nothing to write to */
#endif
literal = "loud"; /* moving the pointer is fine; it is only an address */
printf("literal: %s\n", literal);
return 0;
}