Pointers in C++

Pointers in C++ are used to point to a location in memory. The memory address is stored in the pointer. Suppose we have a temperature variable of type float and a pointer which points to a variable of type float.

// Declare a temperature variable
float temperature;

// Declare a pointer
float* pointerTemperature;

The reference operator (&) can be used to assign a memory address to the pointer.

// Point to the temperature variable
pointerTemperature = &temperature;

pointerTemperature now points to the temperature variable. So how can we be sure that the pointer does in fact point to the temperature variable?

// pointerTemperature points to the memory address of the temperature variable
printf("Temperature memory address: %p\n", &temperature);
printf("Pointer pointing to: %p\n", pointerTemperature);

Output:

Temperature memory address: 0x7fff5fbff8dc
Pointer pointing to: 0x7fff5fbff8dc

The dereference operator (*) can be used to modify the temperature variable via the pointer.

// Dereference the pointer and change the value of the temperature variable
*pointerTemperature = 30.0;
// Print the value of the temperature variable
printf("Temperature: %f\n", temperature);

Output:

Temperature: 30.000000

The snippet below contains the full example code.

#include <iostream>

using namespace std;

void IncreaseTemperature(float* temperature, float amount);
void DecreaseTemperature(float* temperature, float amount);

int main(){

  // Declare a temperature variable
  float temperature;

  // Declare a pointer
  float* pointerTemperature; 

  // Point to the temperature variable
  pointerTemperature = &temperature;

  // Dereference the pointer and change the value of the temperature variable
  *pointerTemperature = 30.0;

  printf("Temperature: %f\n", temperature);

  // pointerTemperature points to the memory address of the temperature variable
  printf("Temperature memory address: %p\n", &temperature);
  printf("Pointer pointing to: %p\n", pointerTemperature);

  // Increase the temperature using the pointer to temperature
  printf("Increasing temperature with 10 degrees\n");
  IncreaseTemperature(pointerTemperature,10);

  // Print the value of the temperature variable
  printf("Temperature: %f\n", temperature);

  // Decrease the temperature using the pointer to temperature
  printf("Decreasing temperature with 200 degrees\n");
  DecreaseTemperature(pointerTemperature,200);

  // Print the value of the temperature variable
  printf("Temperature: %f\n", temperature);
}

void IncreaseTemperature(float* temperature, float amount){
  *temperature+=amount;
}

void DecreaseTemperature(float* temperature, float amount){
  *temperature-=amount;
}

Output:

$ g++ -o pointers pointers.cpp
$ ./pointers
Temperature: 30.000000
Temperature memory address: 0x7fff5fbff8dc
Pointer pointing to: 0x7fff5fbff8dc
Increasing temperature with 10 degrees
Temperature: 40.000000
Decreasing temperature with 200 degrees
Temperature: -160.000000

 

 

 

 

 

This entry was posted in C++ and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?