Skip to content

Instantly share code, notes, and snippets.

@AssafTzurEl
Created June 14, 2019 08:33
Show Gist options
  • Save AssafTzurEl/e5e81b0fac27782838f417fba68bd0ad to your computer and use it in GitHub Desktop.
Save AssafTzurEl/e5e81b0fac27782838f417fba68bd0ad to your computer and use it in GitHub Desktop.
Const in C
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