Creating and Initializing Pointers
Contents
Pointer Definition :
A pointer is a variable which stores the memory address of another variable of same data type.
Let us try to explore this definition with a particular example :
int a = 32 ;// a simple variable being initialized with a value equal to 32
When the above line is being executed by the compiler, it will allocate the variable a with a random hexadecimal address say: 0x8fbffff4. This memory address will remain attached with the variable a till the variable is present in the program scope.
Try to Do:
Copy the above hexadecimal memory addresses and convert it using calculator program to its decimal form.
Write the decimal equivalent in your notebook.
We are observing that the addresses allocated to simple variables are themselves a type of values ( i,e hexadecimal values).So a question arises that can’t address values be stored again in some variables? If yes, then what would be the data type of those variables? Whether those variables would be like simple variables or different from it? What would be the size of those variables?
Let us try to sort out the answers of all of the above questions.Since a variable which is storing an address of another variable, must be a special one, so in C++ these variables are treated specially and are declared using special syntaxes.
Pointer Declaration:
A pointer declaration is similar to the declaration of a simple programming variable except that it is declared with a (*) asterisk symbol. The syntax is as follows :
<data type> * <Pointer variable_name>
For eg.:
int * iptr ; // an integer pointer variable
char * cptr ; // a character pointer variable
float * fptr ; // a float pointer variable
Pointer Initialization:
We know that a simple variable is initialized with a value using assignment operator ( = ) like :
int a = 20 ; // a simple variable initialized by a value equal to 20
Likewise whenever we declare a pointer variable we must initialize it with a particular value using the same assignment operator (=). But as we know from earlier definition of pointers that a pointer variable only holds an address of another variable, so the value assigned to a pointer must be an address of another simple variable of same data type.
Extracting Address of a simple variable :
One of the biggest question arises that : How to extract or find the address of a simple variable so that it could be assigned to a pointer variable. For This we have a special operator for this purpose i,e. ( & ) ampersand ( read it as address of operator ). This is a unary operator whose sole motive is to return the address of the variable which is present in the R.H.S of it.
Consider the following code :
int a = 20 ;// Line No. 1
int * ptr = &a;// Line No. 2
The Line No. 1 of the above code snippet creates an ordinary variable a having an integer value equal to 20.
The Line No. 2 of the above code snippet creates a Pointer variable ptr having a value assigned to it. We observe that the value assigned to it is the“address of a” ( i,e. &a). So from the above code it is inferred that a pointer variable (ptr) must be initialized with address of a simple variable (a).