Created
June 14, 2019 08:33
-
-
Save AssafTzurEl/e5e81b0fac27782838f417fba68bd0ad to your computer and use it in GitHub Desktop.
Const in C
This file contains 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
void main() | |
{ | |
const int ANSWER = 42; | |
int i = 0; | |
//ANSWER = 43; // Can't assign to constant | |
const int *cp; | |
cp = &ANSWER; | |
//*cp = 43; // cp points to a (const int) | |
cp = &i; // cp's contract allows to modify its contents | |
i = 1; | |
//*cp = 2; // cp's contract is not to modify the referenced value | |
// Assignint to const, the evil way: | |
int *p; | |
p = &ANSWER; // Warning only (error in C++) | |
*p = 43; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment