Skip to content

Instantly share code, notes, and snippets.

@aprell
Created July 4, 2013 14:21
Show Gist options
  • Select an option

  • Save aprell/5928178 to your computer and use it in GitHub Desktop.

Select an option

Save aprell/5928178 to your computer and use it in GitHub Desktop.
Storage for string constants
#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;
}
@aprell
Copy link
Copy Markdown
Author

aprell commented Jul 4, 2013

C: A Reference Manual, 5th Edition, Sec. 2.7 Constants, pp. 33-34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment