Created
July 4, 2013 14:21
-
-
Save aprell/5928178 to your computer and use it in GitHub Desktop.
Storage for string constants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(void) | |
| { | |
| char a[] = "Always writable"; | |
| char *b = "Likely not writable"; | |
| const char c[] = "Never writable"; | |
| char *x = "xyz"; | |
| char *y = "xyz"; | |
| if (x == y) { | |
| printf("String constants are shared\n"); | |
| } else { | |
| printf("String constants are not shared\n"); | |
| } | |
| a[7] = 'W'; // Okay | |
| b[7] = 'N'; // Runtime error if string is allocated in read-only memory | |
| c[7] = 'R'; // Compile-time error | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C: A Reference Manual, 5th Edition, Sec. 2.7 Constants, pp. 33-34