Discussion: Time to Pull Out Your Hair
Execute the code to understand the output and gain insights into pointer modification.
We'll cover the following...
Run the code
Now, it's time to execute the code and observe the output.
#include <stdio.h> void modify(char *c) { *(c+1) = 'o'; } int main() { char *string = "cat"; printf("Before: %s\n", string); modify(string); printf("After: %s\n", string); return(0); }
C code for the given puzzle
Understanding the output
The output might be a surprise:
Before: catSegmentation fault (core dumped)
Code output
Yes, it’s a pointer problem. It’s just not the pointer problem you may think it is. The issue lies in the construction of the ...