Skip to content

Instantly share code, notes, and snippets.

@Samathy
Created January 16, 2018 12:38
Show Gist options
  • Save Samathy/d92f31c55ada1154a37e58a9e2c036f7 to your computer and use it in GitHub Desktop.
Save Samathy/d92f31c55ada1154a37e58a9e2c036f7 to your computer and use it in GitHub Desktop.
Constant Confusion
int main()
{
char character = 'f'; //A single character
char anotherCharacter = 'b';
static char staticCharacter = 'f'; /* A variable must be static to be evaluated
at compile time. */
static char * cptr = &character; /* A pointer to a character.
One can modify the data,
or the address */
cptr = &anotherCharacter; //Valid Point to a different address
*cptr = 'z'; //Valid Change the data in the address we point too.
static const char * ccptr = &character; /* [1] A pointer to a constant character
One can change the pointed too
address but you can't change the data
at that address */
ccptr = &anotherCharacter; //Valid Point to a different address
*ccptr = 'z'; //Invalid - Can't change the data in the address we point too.
static char * const cptrc = &character; /* [2] A constant pointer to a character
One can't change where cptrc points
to, but you can change the data at the
memory address it points to. */
cptrc = &anotherCharacter; //Invalid - Can't point to a different address
*cptrc = 'z'; //Valid - Change the data in the address we point too.
static const char * const ccptrc = &character; /* [3] A constant pointer to a
constant character.
One can't change where ccptrc
points to, or the data at the
address */
ccptrc = &anotherCharacter; //Invalid - Can't point to a different address
*ccptrc = 'z'; //Invalid - Cant change the data in the address we point too.
static constexpr char * cxcptr = &staticCharacter; /* [4] A compile time
evaluated pointer to a
character. Behaves the
same as [2] */
cxcptr = &anotherCharacter; //Invalid - Cant point to a different address
*cxcptr = 'z'; //Valid - Change the data in the address we point too.
static constexpr char * const cxcptrc = &staticCharacter; /* [5] A compile time
evaluated constant pointer
to a character. Behaves
the same as [2] and [4]
because constexpr pointers
are always const */
cxcptrc = &anotherCharacter; //Invalid - Can't point to a different address
*cxcptrc = 'z'; //Valid - Change the data in the address we point too.
static constexpr const char * cxccptr = &staticCharacter; /* [6] A compile time
evaluated pointer to a
constant character.
Behaves the same as [3] */
cxccptr = &anotherCharacter; //Invalid - Cant point to a different address
*cxccptr = 'z'; //Invalid - Can't change the data in the address we point too.
static constexpr const char * const cxccptrc = &staticCharacter; /* [7] A compile
time evaluated
constant
pointer to a
constant
character.
Behaves the
same as [6] */
cxccptrc = &anotherCharacter; //Invalid - Can't point to a different address
*cxccptrc = 'z'; //Invalid - Can't change the data in the address we point too.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment