- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0
Pointers:
A type of variable that is used to store the memory address of a memory cell is known as pointer. It normally stores the memory address of a variable or object. The data type of a pointer must be same as the data type of the variable whose memory address is stored in pointer. In C++ we can refer a variable indirectly that is by using its address.
NOTE: A pointer can be a NULL pointer and its value is zero.
EXAMPLE
The following program demonstrates the use of pointers.
#include<iostream>
Using namespace std;
Void main()
{
int *var;
int num=25;
var = #
cout<<”address of variable num is”<<#
cout<<endl<<”contents of num”<<num;
cout<<endl<<”address of memory location pointed to by var is”<<var;
cout<<”contents of memory pointed by var is”<<*var;
}
Working of the above program:
The pointer var stores the memory address of num. It does not store its value. The ampersand ‘&’ is used to assign the memory address of num to var. The memory address fff4 in the output may be different in each execution of the program. It is because the memory addresses are assigned when the program is executed.
File pointer
File pointer is a pointer that refers to a file in a secondary storage. It is a variable of type FILE that is defined in the header file <stdio.h> in turbo C++. It is used to access and manipulate a data file. A program has to declare a file pointer to use a file. The file pointer is associated with a file after declaration. One file pointer can be associated with only one data file.
SYNTAX
The syntax for declaring a file pointer is as follows:
FILE *MyFile;
The identifier MyFile is a file pointer. FILE is the type of MyFile pointer. The symbol ‘*’ indicates that it is pointer to file structure. This structure is defined in stdio.h header file. It contains all information that is required to manage files. It must be included in the program to manipulate files.
Uses of Pointers in C++
Functions or modules in C++ programming language use arguments or parameters, to which the user has to pass values. Sometimes, it becomes difficult to pass the values of the parameters; in such cases pointers are used.
Pointers are also often used to return more than one value from a function. This can also be done by using structures (will be discussed later). For example:
#include<iostream>
Using namespace std;
Void point(int *, int * );
Void main()
{
Int a, b;
Point(&a, &b);
Cout<<a<<endl<<b;
}
Void point(int * , int * )
{
*pa= 4;
*pb=3;
}
Working of the above program:
In the main () function two variables are created that have some memory locations and addresses. The address of each variable is passed through parameters in the function. The call of the sub function that is point(int &a, int &b) gets values from the function . The working of this function is very simple. It just provides or sets the values or contents of the variables a and b and return the values. After this cout function prints the value of the variables a and b.
As it is clear from the above program that we passed the addresses of the variables therefore, the values of the variables are stored in these addresses or memory locations. These memory locations have been given the name of pa and pb. Therefore, the addresses of variables are called pointer constants, and a pointer variable is used to store the address.
This method is also called calling functions by pointers.
Declaring Pointers
We have already learnt that to use a variable in a program, we have to declare it first else the compiler will generate an error message. Likewise, pointers are also declared before using in the program, this will also help the compiler to know how much memory is allocated to the pointer. For simple variables we use data types to define them. You may regard that a pointer also has a data type, the syntax to define or declare a pointer is as follows:
SYNTAX
Data_type *variableName;
Data_type is the primitive type that can be an integer.
‘*’ Indicates that the declared variable is a pointer.
VariableName is the name of the variable.
Pointer can also be declared using the keyword “ptr” that is
ptr a, b;
This allocates two bytes of the memory to the pointer variable. In C++ programming instead of using the “ptr”, we use ‘*’ for the pointers. Ptr is used at the beginning of the pointer variable declaration but ‘*’ is used immediately before the pointer variable.
NOTE: ‘*’ is a unary operator that is it works only on one operand. (Such as *a)
The following figure demonstrates the concept of pointers:
Passing Values to pointers
In the above example x and y are pointer variables, the function is called by the two addresses &a and &b, the control is transferred to the function and these addresses too. In the function these addresses are stored in the pointer variables.
Pointers and arrays
Pointers and arrays are sometimes used together to get the address of the variable and its value at the same time to print the addresses of variables.
EXAMPLE
Program to display address using arrays:
# include <iostream>
Using namespace std;
Void main()
{
Int array[2];
Cout<<”addresses of variables”;
For(int i=0; i<2; i++)
{
Cout<<&array[i]<<endl;
}
The address of the array is simply printed by using ‘&’ with the array variable.
Now demonstrating the program to display address using pointers:
# include<iostream>
Using namespace std;
Void main ()
{
Float array[2];
Float *point;
Point = array[1];
Cout<<”address of second element of the array = ”
Cout<<point;
}
The line point =array[1] shows that we are referring to the second element of the array that is array[1]. In the cout statement the address is printed.