C++ Pointers

C++ Pointers are variables that store memory address of another variable. In C++ programming language, each variable that user declares, has a specific memory allocation. To access these memory locations programmers user pointers. The syntax for declaring a pointer is:

Asterisk sign (*) represents the pointers. It is called the Deference operator. Here pointer_type is the data type of pointer having name pointer_name. There are more than one syntaxes for declaring pointers. The second syntax is adding deference operator next to the pointer data type such as.

Here is another way of declaring a pointer along with a variable.

In the above code, the pointer does not point to any variable yet. In order to assign memory location, programmer needs to point the pointer to a specific variable. The syntax for assigning value to pointer is:

Assignment operator (=) is used to assign address value to the pointer. The correct syntax includes Ampersand sign (&) before the variable name.

Consider a variable var of type int. The pointer to the variable is ptr that also has type int. Programmer can declare and initialize the pointer in the following way:

The pointer contains hexadecimal numbers that is memory address of the variable. The simple code below shows how to print the value of pointer.

#include <iostream>
using namespace std;
int main()
{
  int var, *ptr;
  var = 5;
  ptr = &var;
  cout << "The memory address is: " << ptr;
  return 0;
}

The output of above code after compilation is:

Here is another example that shows the difference between values of variables, and pointers.

#include <iostream>
using namespace std;
int main()
{
  int var, *ptr;
  var = 5;
  ptr = &var;
  cout << "The value of (ptr) is: " << ptr << endl;
  cout << "The value of (*ptr) is: " << *ptr << endl;
  cout << "The value of (&var) is: " << &var << endl;
  cout << "The value of (var) is: " << var << endl;
  return 0;
}

The output of the above code after compilation is.

Note that in the above example, values in ptr and &var is same. Both contain the memory address of the variable var. Whereas, the values of both *ptr and var are same.

Pointer Data Types

Pointers have data types. Their data types are similar to those of variables. Pointer can have following data types:

  • int
  • float
  • double
  • char
  • string

Note that the data type of variable and its pointer should be same. The example below shows pointers and variables having different data types.

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int var1 = 5, *ptr1;
  string var2 = "string", *ptr2;
  double var3 = 97.4, *ptr3;
  float var4 = 3.14, *ptr4;
  ptr1 = &var1;
  ptr2 = &var2;
  ptr3 = &var3;
  ptr4 = &var4;
  cout << "The value of (ptr1) for var1 (" << var1 << ") is: " << ptr1 << endl;
  cout << "The value of (ptr2) for var2 (" << var2 << ") is: " << ptr2 << endl;
  cout << "The value of (ptr3) for var3 (" << var3 << ") is: " << ptr3 << endl;
  cout << "The value of (ptr4) for var4 (" << var4 << ") is: " << ptr4 << endl;
  return 0;
}

The output of above code after complilation is.

Assigning Values to Pointers

Programmer can assign different values to pointers. Note that on assigning different values to the variable, does not change the memory allocation of that variable. For example, the code below shows how to assign different values to a pointer.

#include <iostream>
using namespace std;
int main() {
    int var = 2;
    int* ptr;
    ptr = &var;
    cout << "var = " << var << endl;
    cout << "*ptr = " << *ptr << endl;
    cout << "ptr = " << ptr << endl;
    var = 4;
    cout << "var = " << var << endl;
    cout << "*ptr = " << *ptr << endl;
    cout << "ptr = " << ptr << endl;
    *ptr = 8;
    cout << "var = " << var << endl;
    cout << "*ptr = " << *ptr << endl;
    cout << "ptr = " << ptr << endl;
    return 0;
}

The output of above code after compilation is:

The above code initializes the variable var at 2. After that it changes the value of variable var to 4. Then it changes the value of pointer *ptr to 8.

Pointer to Arrays

Just like variables, pointers can point to arrays as well but the syntax is different. The syntax for declaring array pointer and assigning it array memory address is.

Pointer to an array points to the base address of array. The syntax for initializing pointer to array omits the ampersand sign (&). It only uses the assignment operator (=). For example, the code below shows an array having name arr and a pointer ptr of type int.

The address of element at each array index is different. Programmers can use loops to traverse arrays and get address of each element and save it in unique pointer. Here is an example that shows an int array having 5 elements. The code gets the address of each element by array traversal.

#include <iostream>
using namespace std;
int main(){
   int *ptr;
   int arr[]={5, 10, 15, 20, 25, 30};
   ptr = arr;
   cout << "The base address of array is :" << ptr << endl;
   for(int i=0; i<6;i++){
     cout << "The value at array index " << i << " is: " << *ptr 
     << " and its address is: " << ptr << endl;
     ptr++;
   }
   return 0;
}

The output of above code after compilation is.

Pointer to Structures

Like other variables (int, float, char) structures can also have C++ pointers. The pointer points to the structure’s object. The syntax for creating a pointer to structure is:

Instead of using dot operator (.) to access structure members, with pointers it is necessary to use arrow operator (->). For example, the below code shows pointer to structure.

#include <iostream> 
using namespace std; 
struct Location { 
    int Longitude;
    int Latitude;
};
int main() 
{ 
    struct Location loc1 = { 10, -20 }; 
    struct Location* loc2 = &loc1; 
    cout << loc2->Longitude << " " << loc2->Latitude; 
    return 0;
}

The output of above code after compilation is: