Skip to content

Instantly share code, notes, and snippets.

@bhargavkulk
Created September 16, 2018 11:58
Show Gist options
  • Save bhargavkulk/904d15a3c24a978c7865fab3632dd88b to your computer and use it in GitHub Desktop.
Save bhargavkulk/904d15a3c24a978c7865fab3632dd88b to your computer and use it in GitHub Desktop.
#include<iostream.h>
#include<conio.h>
int const p = 5;// both are the same
const int q = 6;// ^
int r = 7;
int main() {
//////////////////////////////////////////////
/* pointer to const */
int const *ptr = &p // both are the same
const int *qtr = &q;// ^
*ptr += 1; //not allowed
/*
ptr points to an immutable (unchangable)
memory location
*/
ptr++; //allowed
/*
ptr currently points to an unchangable
memory address, but it can still move
around
*/
//////////////////////////////////////////////
/* const pointer */
int *const rtr = &r;
*rtr += 1; // allowed
/*
rtr points to a mutable (changeble)
memory location
*/
rtr++; // not allowed
/*
rtr is a constant pointer;
it can't move around
*/
//////////////////////////////////////////////
/* const pointer to const */
const int *const str = &p;// both are the same
int const *const ttr = &q;// ^
*str += 1 //not allowed
/*
str points to an immutable (unchangable)
memory location
*/
str++;
/*
str is a constant pointer;
it can't move around
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment