C++ - SPLessons

C++ Pointers

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Pointers

C++ Pointers

Pointers

shape Description

Any compiler need to store the variables in a memory. Basically, the data given in these variables can be retrieved by using ‘&‘. The memory address of data stored in a particular variable is called as a pointer.

Simply, CPP Pointers are memory locators of a variable. So, by using pointers the variable can be accessed. Pointers play a key role in exempting C-language from other programming languages like java, Visual Basic,etc.

shape Conceptual
figure

In the above figure, a is the variable having value 10 located at an address 1000. This address is stored in another variable “ptr” called as pointer variable located at an address 2000. So, the datatype should be same for normal variable and pointer variable.

In C-language, the address of a variable can obtained by giving “&” in front of the variable like i=&a.

shape Syntax

(pointer_type)  *(pointer_name);

(or)

(pointer_type)*  (pointer_name);

shape Example

char *ptr;  (or)  char*  ptr;

shape Examples

#include <iostream>
using namespace std;

int main ()
{
   int  a = 20;   // normal variable
   int  *p;       // pointer variable 

   p = &a;       // store address of 'a' in pointer variable

   cout << "value of given variable is: " << a << endl;

   // printing the address of normal variable using &
   cout << "Address of variable a is "<< p << endl;

   //Accessing the variable a by pointing to address of p variable using *
   cout << "Value of a is : "<< *p<< endl;
 
   //Pointing the address of normal variable 'a' using pointer variable p
   cout << "Address of a is : " << &(*p) << endl;
   
   //Printing address of pointer variable 'p'
   cout << "Address of p is : " << &(p) << endl;
   return 0;
}

Output:

value of given variable is: 20
Address of variable a is 0x23fe3c
Value of a is : 20
Address of a is : 0x23fe3c
Address of p is : 0x23fe30

Pointers and Arrays

shape Description

The value of particular element in an array can be changed by using pointers. Basically, arrays are formed by incrementing the address of previous element. But, CPP Pointers does not point the address of array by incrementing the address of previous element. It follows the data type given to the array.

Suppose, if ‘int’ is declared as data type of array and the size of array is 5. When used pointers to access the 2nd element of array, the compiler adds 2 to 1st element address because array is of ‘int’ type having size 2 bytes.

shape Example


#include <iostream>
using namespace std;

int main() 
{
    int a[3],*p;
    
// Inputting data using pointers
    cout << "Enter 3 numbers: ";
    for (int i = 0; i < 3; ++i) { cin >> *(a+i) ;
    }

// Displaying data using pointer notation
    cout << "Displaying data: " << endl;
    for (int i = 0; i < 3; ++i) 
    {
        cout << *(a+i) << endl ;
    }
 
    p=a;

    cout<<"\nDisplaying address of given variables: "<< endl;
    for (int i = 0; i < 3; ++i) 
    {
        cout << "p + " << i << " = "<< p+i <<endl;
    }
    return 0;
}

Output

Enter 3 numbers: 1
2
3
Displaying data:
1
2
3

Displaying address using pointers:
ptr + 0 = 0x23fe10
ptr + 1 = 0x23fe14
ptr + 2 = 0x23fe18

Pointers and Functions

shape Description

In functions chapter, the value of variable is passed to function by using ‘pass by value‘ and ‘pass by reference‘. There, pass by reference uses pointers to send the variables. Below is an other example showing the relation between pointers and functions. 

shape Example

#include <iostream>
using namespace std;

int main() 
{
    void swap(int*, int*);      // Function prototype
    int x = 1, y = 2;

    swap(&x, &y);

    cout << "\nValues after swapping" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    return 0;
}

void swap(int* a, int* b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

Output

Values after swapping
x = 2
y = 1

Summary

shape Key Points

  • Pointer, a variable, points to address of a another variable.
  • Specification of pointing address is must.
  • When using CPP Pointers with arrays, it increments the value based on the data type.

shape Programming
Tips

  • Never forget to initialize pointer because it results in many bugs.
  • Take care that a single pointer does not point towards multiple memory locations.