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
Consider the following figure.
- 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,
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:
- int * arr;
- int size, i;
- cout << “Enter the size of the array” << endl;
- cin >> size;
- arr = new int[size];
- for (i = 0; i < size; i++)
- {
- cout << “Enter value” << i + 1 << endl;
- cin >> arr[i];
- }
- cout << “The contents of the array are” << endl;
- for (i = 0; i < size; i++)
- {
- cout << arr[i] << endl;
- }
- delete[] arr;
Using Pointers with Functions
In C++, you can:
- Pass pointers to functions.
- Return pointers from functions.
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:
- #include < iostream.h >
- void swap(int * , int * );
- void main()
- {
- int number1, number2;
-
- swap( & number1, & number2);
- }
- void swap(int * ptr1, int * ptr2)
- {
- int temp;
- temp = * ptr1; * ptr1 = * ptr2; * ptr2 = temp;
- }
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:
- #include < iostream.h >
- int * FindSmallest(int & , int & );
- void main()
- {
- int x, y, * z;
-
- z = FindSmallest(x, y);
- }
- int * FindSmallest(int & a, int & b)
- {
- if (a < b)
- return &a;
- else
- return &b;
- }
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:
- int score[10];
- cout << “Input Scores
- for 5 subjects: ”;
- for (int j = 0; j < 5; j++)
- {
- cin >> score[j];
- }
- 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:
- structEmployee_details
- {
- intEmployee_ID;
- intEmployee_Salary;
- } * 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:
- Employee_detailsEmp_var;
- 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:
- void main()
- {
- structEmployee_details
- {
- intEmp_id;
- intEmp_salary;
- }
- Emp_var =
- {
- 100,
- 5000
- };
- Employee_details * employee;
- employee = & Emp_var;
- cout << endl << employee - > Emp_id;
- cout << endl << employee - > Emp_salary;
- }
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:
- void main()
- {
- intarr[6] =
- {
- 10,20,30,40,50,60
- };
- int * ptr1;
- ptr1 = arr;
- cout << “\n” << * ptr1;
- ptr1 = ptr1 + 1;
- cout << “\n” << * ptr1;
- }
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:
- void main()
- {
- intarr[6] =
- {10,20,30,40,50,60};
- int * ptr1;
- ptr1 = arr;
- cout << “\n” << * ptr1;
- ptr1 = ptr1 + 2;
- cout << “\n” << * ptr1;
- ptr1--;
- cout << “\n” << * ptr1;
-
- }
Subtracting a Pointer from another Pointer
- void main()
- {
- intarr[6] =
- {10,20,30,40,50,60};
- int * ptr1, * ptr2;
- ptr1 = arr;
- ptr2 = ptr1 + 2;
- cout << “\n” << ptr2 - ptr1;
- }
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:
- void main()
- {
- intarr[6] = {10,20,30,40,50,60};
- int * ptr1, * ptr2;
- ptr1 = arr;
- ptr2 = arr;
- if (ptr1 > ptr2)
- {
- cout << “ptr1 is greater” << endl;
- } else if (ptr2 > ptr1)
- {
- cout << “ptr2 is greater” << endl;
- } else
- {
- cout << “Both pointers are equal” << endl;
- }
- }