C++ Pointers

We can define a variable in C++ to store a memory address. A pointer in C++ is said to “point to” the memory address that is stored in it. Also, when defining a C++ pointer variable, we must specify the type of variable to which it is pointing. For example, to define a pointer, which will store a memory address at which exists an int, we can do the following:

//Sample program for c++ pointer
signed main()
{
int* p;
//Right now, p contains no particular myval in this C++ code.
}

The asterisk in the above specifies that we have a pointer variable.    Let’s say we want to define an int variable and then we want to define a pointer variable, which will store the memory address of this int:

//c++ pointer using an int variable

signed main()
{
int myval(7);
int* p_myval;
//Right now, p_myval contains no particular myval.
p_myval = &myval;
//Now, p_myval in this c++ program contains the memory address of the variable myval
}

With &myval, & is referred to as “the address-of operator”. The expression &myval is of the c++ type int*. We then store this int* myval in our int* variable, which is p_myval.    Now, we will actually use this pointer:

//Sample program for c++ pointer 
signed main()
{
int myval = 7;
int* p_myval = &myval;
*p_myval = 6;
}

With *p_myval = 6, the asterisk is referred to as “the dereference operator”. It turns the expression from an int* into an int. The statement has the effect of setting the myval of myval to 6.    So now what are the uses of pointers in c++? Let us see something about how and when they should be used:

A) When the pointer must be re-seated.

B) When arrays are involved.

Consider a string, an array of characters:

signed int main()
{
char my_name[] = "Code";
}

Here’s what the string (char c++ pointer) looks like in memory:

Type of VariableAddress in MemoryValue Stored Char108’C’ char109’o’ char110’d’ char111’e’ char112”

Name of Variable
my_name

While accessing the characters inside the variable my_name, the position of the first character will start from 0. So the array of size 4 will be accessed for characters from 0, 1, 2 and 3. We can define a pointer to point to the second element in the array my_name, as so:

int main()
{
char my_name[] = "Code";
char* k( &my_name[1] );
}

Now, what k points to looks like so in memory:

Type of VariableAddress in MemoryValue Stored Char*116109 char109’o’ char110’d’ char111’e’ char112”

Name of Variable
my_name

So that’s one usage of c++ pointers there, to point to an individual object in an array.    The other feature of c++ pointers is that they can be “re-seated”, which means that you can change their value, you can change what they’re pointing to, as in the following: // c++ pointer program for modifying values/re-seating.

signed int main()
{
int myval(5);
int myvalue2 = 7;
int* p_primate;
p_primate = &myval;
*p_primate = 9;
p_primate = &myvalue2;
*p_primate = 10;
}

Guess what kind of variable we have in the following:

signed main()
{
signed** p_p_cow;
}

An int* c++ pointer points to an int, so an int** points to an int*. In English: The variable p_cow above stores a memory address. At that memory address exists a variable of type int*. This int* variable also stores a memory address, at which exists an int. Take the following:

//Snippet for c++ pointer to pointers

int main()
{
int cow(7);
int* p_cow = &cow;
int** p_p_cow(&p_cow);
int*** p_p_p_cow = &p_p_cow;
}

Here’s what the above c++ pointers look like in memory:


Type of VariableAddress in MemoryValue Stored Int1087 int*110108 int**112110 int***114112 p_cowp_p_cowp_p_p_cow

Name of Variable
Cow

With the above code, we can set the value of cow using p_p_p_cow: //Using c++ pointer to pointer

int main()
{
int cow(7);
int* p_cow = &cow;
int** p_p_cow(&p_cow);
int*** p_p_p_cow = &p_p_cow;
***p_p_p_cow = 8;
}

C++ Pointers are commonly used when working with strings. Let’s define a function; this function will be supplied with a string. We’re going to change the 2nd, 5th and 7th characters of the string:

void ChangeString(char* const p_first_char)
{
p_first_char[1] = 'a';
p_first_char[4] = 'b';
p_first_char[6] = 'c';
}

Or we can define a function, which will be supplied with a string. The function will return the first instance of the character ‘t’ in the string:

char* GetFirstT(char* p_first_char)
{
for ( ; *p ; ++p)
{
if ( *p == 't' ) return p;
}
return 0;
}

signed main()
{
char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet);
}

Now I’m going to talk about c++ pointers and constness. If you want a const c++ pointer variable, a c++ pointer variable whose value you can’t change after initialization, then stick the const directly beside the variable’s name:

signed main()
{
int myval = 5;
int myvalue2(8);
int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 3; //No problem, the variable to which it points is non-const
}

If you want a non-const c++ pointer variable, but you want the variable to which it points to be const, then:

signed main()
{
int myval(7);
int myvalue2 = 6;
const int* p_k = &myval;
p_k = &myvalue2; //No problem, the variable is non-const
*p_k = 7; //COMPILE ERROR
}

If you want a const c++ pointer that points to a const variable then:

signed int main()
{int myval(17);
int myvalue2 = 4;
const int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 32; //COMPILE ERROR
}