You are on page 1of 9

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:

Name of Variable Type of Variable Address in Memory Value Stored


my_name Char 108 'C'
  char 109 'o'
  char 110 'd'
  char 111 'e'
  char 112 '\0'
   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:

Name of Variable Type of Variable Address in Memory Value Stored


my_name Char* 116 109
  char 109 'o'
  char 110 'd'
  char 111 'e'
  char 112 '\0'
   So that's one usage of c++ pointers there, to point to an individual object in an array.    The
other feature of c++ pointersis 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:

Name of Variable Type of Variable Address in Memory Value Stored


Cow Int 108 7
p_cow int* 110 108
p_p_cow int** 112 110
p_p_p_cow int*** 114 112
   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

 Pointers and arrays


Pointers and arrays are intricately linked in the C language. In previous lessons, you learned how to
declare an array of variables:

1 int anArray[5]; // declare array of 5 integers

anArray is actually a pointer that points to the first element of the array! Because the array variable is
a pointer, you can dereference it, which returns array element 0:

1 int anArray[5] = { 9, 7, 5, 3, 1 };

2  

3 // dereferencing an array returns the first element (element 0)

4 cout << *anArray; // prints 9!

5  

6 char szName[] = "Jason"; // C-style string (also an array)

7 cout << *szName; // prints 'J'

Pointer arithmetic

The C language allows you to perform integer addition or subtraction operations on pointers. If pnPtr
points to an integer, pnPtr + 1 is the address of the next integer in memory after pnPtr. pnPtr - 1 is
the address of the previous integer before pnPtr.

Note that pnPtr+1 does not return the address after pnPtr, but the next object of the type that pnPtr
points to. If pnPtr points to an integer (assuming 4 bytes), pnPtr+3 means 3 integers after pnPtr,
which is 12 addresses after pnPtr. If pnPtr points to a char, which is always 1 byte, pnPtr+3 means 3
chars after pnPtr, which is 3 addresses after pnPtr.

When calculating the result of a pointer arithmetic expression, the compiler always multiplies the
integer operand by the size of the object being pointed to. This is called scaling.

The following program:


1 int nValue = 7;

2 int *pnPtr = &nValue;

3  

4 cout << pnPtr << endl;

5 cout << pnPtr+1 << endl;

6 cout << pnPtr+2 << endl;

7 cout << pnPtr+3 << endl;

Outputs:

0012FF7C
0012FF80
0012FF84
0012FF88

As you can see, each of these addresses differs by 4 (7C + 4 = 80 in hexadecimal). This is because an
integer is 4 bytes on the author’s machine.

The same program using short instead of int:

1 short nValue = 7;

2 short *pnPtr = &nValue;

3  

4 cout << pnPtr << endl;

5 cout << pnPtr+1 << endl;

6 cout << pnPtr+2 << endl;

7 cout << pnPtr+3 << endl;

Outputs:
0012FF7C
0012FF7E
0012FF80
0012FF82

Because a short is 2 bytes, each address differs by 2.

It is rare to see the + and – operator used in such a manner with pointers. However, it is more
common to see the ++ or — operator being used to increment or decrement a pointer to point to the
next or previous element in an array.

Pointer arithmetic and arrays

If anArray is a pointer that points to the first element (element 0) of the array, and adding 1 to a
pointer already returns the next object, then anArray+1 must point to the second element (element 1)
of the array! We can verify experimentally that this is true:

1 int anArray[5] = { 9, 7, 5, 3, 1 };

2 cout << *(anArray+1) << endl; // prints 7

The parentheses are necessary to ensure the operator precedence is correct — operator * has higher
precedence than operator +.

Note that *(anArray+1) has the same effect as anArray[1]. It turns out that the array indexing
operator ([]) actually does an implicit pointer addition and dereference! It just looks prettier.

We can use a pointer and pointer arithmetic to loop through an array. Although not commonly done
this way (using indices is generally easier to read and less error prone), the following example goes to
show it is possible:

01 const int nArraySize = 7;

02 char szName[nArraySize] = "Mollie";

03 int nVowels = 0;

04 for (char *pnPtr = szName; pnPtr < szName + nArraySize; pnPtr++)

05 {

06     switch (*pnPtr)

07     {
08         case 'A':

09         case 'a':

10         case 'E':

11         case 'e':

12         case 'I':

13         case 'i':

14         case 'O':

15         case 'o':

16         case 'U':

17         case 'u':

18             nVowels++;

19             break;

20     }

21 }

22  

23 cout << szName << " has " << nVowels << " vowels" << endl;

This program uses a pointer to step through each of the elements in an array. Each element is
dereferenced by the switch expression, and if the element is a vowel, nVowels is incremented. The for
loop then uses the ++ operator to advance the pointer to the next character in the array. The for loop
terminates when all characters have been examined.

The above program produces the result:

Mollie has 3 vowels

You might also like