Created
January 30, 2023 18:49
-
-
Save pavly-gerges/0b286f828e5c2c83b9f976c376369cc1 to your computer and use it in GitHub Desktop.
Constant pointer to non-constant data
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 x = 8; // define x | |
int y; // define y | |
// constant location (pointer) to non-constant data | |
int* const ptr = &x; | |
// an attempt to modifiy the data will succeed | |
*ptr = 55; | |
printf("%d\n", x); | |
// an attempt to modify the memory location will fail | |
ptr = &y; /*error: assignment of read-only variable 'ptr'*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment