Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Created January 30, 2023 18:49
Show Gist options
  • Save pavly-gerges/0b286f828e5c2c83b9f976c376369cc1 to your computer and use it in GitHub Desktop.
Save pavly-gerges/0b286f828e5c2c83b9f976c376369cc1 to your computer and use it in GitHub Desktop.
Constant pointer to non-constant data
#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