Last active
December 19, 2015 08:58
-
-
Save aprell/5929178 to your computer and use it in GitHub Desktop.
Constant pointers and pointees
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) | |
| { | |
| 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; | |
| } |
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. 4.4 Type Specifiers and Qualifiers, pp. 89-91