C++ Refresher - Part Four

You can visit the previous parts of the article here:

Here is part four o the C++ Refresher.

In this session, you will learn about:

  • Using pointers with functions
  • Pointers and arrays
  • Pointers to structures
  • Pointer arithmetic
Working with Pointers

pointers

Consider the following figure.

value

memory
  • In C++, you can manipulate a variable by using its address. This can be achieved by using pointers.
  • A pointer is a variable that stores the location/address of another variable.
  • A pointer is declared by using the ‘*’ (asterisk) operator, also known as the indirection operator or the deference operator.

As shown in the following code snippet:

charvar = ‘G’;
char *ptr;
ptr = &var;

The following figure shows the schematic representation of the variable, var and the pointer variable, ptr in the memory,

memory

To print the value of the variable, var, using the pointer variable, ptr, you need to use the following statement:
cout<<*ptr;

Advantages of using pointers:

  • Allow direct access to individual bytes in the memory; thereby, enabling faster data access
  • Enable dynamic memory allocation.
  • In C++, memory is allocated dynamically with the help of the new operator.
  • The syntax for using the new operator is:
    <type><variable>=new <type>;

The memory allocated using the new operator can be de-allocated, when it is no longer required, with the help of the delete operator.

  • The syntax for using the delete operator is:
    delete<variable>;
Here is the example:
  1. int * arr;  
  2. int size, i;  
  3. cout << “Enter the size of the array” << endl;  
  4. cin >> size;  
  5. arr = new int[size];  
  6. for (i = 0; i < size; i++)  
  7. {  
  8.     cout << “Enter value” << i + 1 << endl;  
  9.     cin >> arr[i];  
  10. }  
  11. cout << “The contents of the array are” << endl;  
  12. for (i = 0; i < size; i++)  
  13. {  
  14.     cout << arr[i] << endl;  
  15. }  
  16. delete[] arr;  
Using Pointers with Functions

In C++, you can:
  • Pass pointers to functions.
  • Return pointers from functions.

concept

The advantage of passing pointers to functions is that the function can directly access the parameters using their addresses.

Passing Pointers to Functions

You can create functions that accept pointers as parameters, as shown in the following code:

  1. #include < iostream.h >  
  2.     void swap(int * , int * );  
  3. void main()  
  4. {  
  5.     int number1, number2;  
  6.     //statements to accept user input  
  7.     swap( & number1, & number2);  
  8. }  
  9. void swap(int * ptr1, int * ptr2)  
  10. {  
  11.     int temp;  
  12.     temp = * ptr1; * ptr1 = * ptr2; * ptr2 = temp;  
  13. }  
Returning Pointers from Functions

Till now you have learned about arrays, structures, and pointers as separate concepts. Let’s us see how pointers can be combined with them.

You can make a function return a pointer, as shown in the following code:
  1. #include < iostream.h >  
  2.     int * FindSmallest(int & , int & );  
  3. void main()  
  4. {  
  5.     int x, y, * z;  
  6.     //statements to accept user input  
  7.     z = FindSmallest(x, y);  
  8. }  
  9. int * FindSmallest(int & a, int & b)  
  10. {  
  11.     if (a < b)  
  12.         return &a;  
  13.     else  
  14.         return &b;  
  15. }  
Pointers and Arrays

An array name is actually a pointer that points to the address of its first element.

For example, if intscore[5] holds 5 values, then the array name, score will contain the address of the first element, score[0], as shown in the following code snippet: 
  1. int score[10];  
  2. cout << “Input Scores  
  3. for 5 subjects: ”;  
  4. for (int j = 0; j < 5; j++)   
  5. {  
  6.     cin >> score[j];  
  7. }  
  8. cout << “Score points to” << * score;  
Pointers to Structures 
  • You can use pointers to refer to user-defined data types, such as structures.
  • The pointers that point to structures are known as structure pointers.
  • The syntax to create a structure pointer is:

    struct_name *struct_pointer;

    You can combine the declaration of a structure and its pointer in a single statement, as shown in the following code example:
    1. structEmployee_details  
    2. {  
    3.     intEmployee_ID;  
    4.     intEmployee_Salary;  
    5. } * employee;  
    Before using the structure pointer, you need to initialize it with the address of a structure variable, as shown in the following code example:
    1. Employee_detailsEmp_var;  
    2. mployee=&Emp_var;  
    You can use the structure pointer to access the structure members by using the arrow operator (->), as shown in the following code snippet:
  1. void main()  
  2. {  
  3.     structEmployee_details  
  4.     {  
  5.         intEmp_id;  
  6.         intEmp_salary;  
  7.     }  
  8.     Emp_var =  
  9.     {  
  10.         100,  
  11.         5000  
  12.     };  
  13.     Employee_details * employee;  
  14.     employee = & Emp_var;  
  15.     cout << endl << employee - > Emp_id;  
  16.     cout << endl << employee - > Emp_salary;  
  17. }

Pointer Arithmetic

The following arithmetic operations can be performed on pointers:

  • Adding a number to a pointer
  • Subtracting a number from a pointer
  • Subtracting a pointer from another pointer
  • Comparing two pointers

You have performed arithmetic operations on variables. Similarly, you can perform arithmetic operations on pointers.

Adding a Number to a Pointer

You can add a number to a pointer to enable the pointer to point to the next location, as shown in the following code snippet:

  1. void main()  
  2. {  
  3.     intarr[6] =  
  4.     {  
  5.         10,20,30,40,50,60  
  6.     };  
  7.     int * ptr1;  
  8.     ptr1 = arr;  
  9.     cout << “\n” << * ptr1; //ptr1=10  
  10.     ptr1 = ptr1 + 1;  
  11.     cout << “\n” << * ptr1; //ptr1=20  
  12. }  
Subtracting a Number from a Pointer

You can subtract a number from a pointer to enable a pointer to point to the previous location, as shown in the following code snippet:
  1. void main()   
  2. {  
  3.     intarr[6] =   
  4.       {10,20,30,40,50,60};  
  5.     int * ptr1;  
  6.     ptr1 = arr;  
  7.     cout << “\n” << * ptr1; //ptr1=10  
  8.     ptr1 = ptr1 + 2;  
  9.     cout << “\n” << * ptr1; // ptr1=30  
  10.     ptr1--;  
  11.     cout << “\n” << * ptr1; //ptr1=20  
  12.   
Subtracting a Pointer from another Pointer
  1. void main()  
  2. {  
  3.     intarr[6] =   
  4.       {10,20,30,40,50,60};  
  5.     int * ptr1, * ptr2;  
  6.     ptr1 = arr; //ptr1=10  
  7.     ptr2 = ptr1 + 2; //ptr2=30  
  8.     cout << “\n” << ptr2 - ptr1; //2  
  9. }  
Comparing two Pointers

Two pointer variables can be compared, provided both the pointer variables point to the same data type, as shown in the following code snippet:
  1. void main()  
  2. {  
  3.     intarr[6] = {10,20,30,40,50,60};  
  4.     int * ptr1, * ptr2;  
  5.     ptr1 = arr; // ptr1=10  
  6.     ptr2 = arr; //ptr2=10  
  7.     if (ptr1 > ptr2) //This is false  
  8.     {  
  9.         cout << “ptr1 is greater” << endl;  
  10.     } else if (ptr2 > ptr1) //This is true  
  11.     {  
  12.         cout << “ptr2 is greater” << endl;  
  13.     } else  
  14.     {  
  15.         cout << “Both pointers are equal” << endl;  
  16.     }  


Similar Articles