Skip to content

Instantly share code, notes, and snippets.

@aprell
Last active December 19, 2015 08:58
Show Gist options
  • Select an option

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

Select an option

Save aprell/5929178 to your computer and use it in GitHub Desktop.
Constant pointers and pointees
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
const int c = 3;
int *pa = &a;
const int *pb = &b;
const int *const pc = &c;
*pa += 1;
*pb += 1; // Error
*(int *)pb += 1; // Oh boy...
pa = (int *)pc; // ^ This
*pa += 1; // c is not so const after all
pc = pb; // Error
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
@aprell
Copy link
Copy Markdown
Author

aprell commented Jul 4, 2013

C: A Reference Manual, 5th Edition, Sec. 4.4 Type Specifiers and Qualifiers, pp. 89-91

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