Topic :Pointers in C++: Declaration | Initialization | Using Pointer



Pointers:

Pointers are a Special type of variables, which can store the Memory address of another variable. They contain a memory address, Not the value of the variable. C++ pointer is Much important in Programming. some C++ tasks are performed more easily with a pointer. The concept of the pointers can be well understood from the following example.

Suppose, we Request someone to take a parcel to the house of a person, named Omar. Here the point of reference is a name. However, if we specifically tell him the number of house and the street Number. then this is a reference by the address.



Pointer

As we Know that, when we declare any variable having any type, the variable reserve some space in different location of computer Memory. and each single location of computer Memory have its own unique Address, which could be accessed using Ampersand ( & ) Operator which denotes an address in Memory. Consider the following Example, which will print the address of the variable defined.



/*Pointer: InfoBrother:*/
 
#include<iostream> 
using namespace std;
 
main()
{
    int a,b; 
    double c;
    float d;
 
    cout<<" The Address of a is: "<< &a <<endl; 
    cout<<" The Address of b is: "<< &b <<endl;
    cout<<" The address of c is: "<< &c<<endl;
    cout<<" The address of d is: "<< &d<<endl;
 
    return 0;
}


In My case, i got these Addresses, in Your case it could be different:


Pointer Declaration:

In C++, When we define a Pointer variable we do so by preceding its Name with an Asterisk ( * ). In C++ we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. For Example:



int* ptr;
int *ptr;
int*ptr;
int * ptr;



When we declare an Pointer, we have several option when typing the asterisk ( * ). White space surrounding it does not matter, so each of the above statements declares an integer pointer.



ptr is the name of our variable. The Asterisk ( * ) informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in Memory. the int says that we intend to use our pointer variable to store the address of an integer. such a pointer is said to "point to" an integer.

In the above Example, its Only declaration of pointer. it's mean that ptr has no value, that is we haven't stored an address in it. in this case, if the declaration is outside of any function, it is Initialized to a value guaranteed in such way that it is guaranteed to not point to any C++ variable Address or function. A pointer initialized in this manner is called a NULL pointer.

The Actual bit pattern used for a NULL pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. To make the source code compatible between various compilers on various system, a macro is used to represent a Null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment statement such as:


ptr = NULL:

Guarantees that the pointer has become a NULL pointer.



In C++, Multiplication ( * ) and the pointer ( * ) symbol are the same. This fact sometimes confuses Newcomers to the C++ language. These Operators have no relationship to each other. Keep in mind that both Ampersand ( & ) and Asterisk ( * ) have a higher precedence than any of the arithmetic operators except the unary minus, with which they have equal precedence.



Pointer Initialization:

We declared an pointer ( ptr ) and variable x and now we want to store the address of variable x into the pointer ptr. in simple we Need to initialize the pointer. for this purpose, we use Address Operator & to assign the address of "x" to pointer "ptr", we write:


ptr = &x;

What the & Operator does is retrieve the address of "x", even though "x" is on the right hand side of the assignment operator "=", and copies that to the contents of our pointer "ptr". Now, Ptr is said to "point to" x. in simple, The above Statement assigns the Memory address of the location "x" to the pointer "ptr".



Using A Pointer:

Advanced C++ Programmers use pointers for many purposes. Sometimes they use a pointer as an alternative to an array Name. Remember that any Array Name is actually a Memory address and that pointers hold Memory addresses. Therefore, a pointer also can hold an Array name, as long as the pointer type and Array type are the same.

Let's have a simple example to understand the concept of Pointers: in this example, we will declare two variables and assign the value and will try to change the values of these variables with the help of pointer:



/*Pointer: InfoBrother*/
 
#include<iostream> 
using namespace std;
main()
{
   int x = 5, y = 15; //variable declaration with value:
   int *ptr; //Pointer: ptr point to int:
 
   //before Pointer:	
   cout<<"Before: x = "<<x<<" | y = "<<y<<endl;
 
   ptr = &x; //copy the address of x into ptr:
   *ptr = 10; //replace the value of x by 10: 
 
   ptr = &y; //copy the address of y into ptr:
   *ptr = 20; //replace the value of y by 20;
 
   //after pointer:
   cout <<"After: x = " << x << " | y = " <<y<<endl;
 
   return 0;
}


In The above Example, did you Notice how the values of X and Y have changed indirectly. First we have assigned to ptr the address of X using the Address of ( & ) Operator. Then we have Assigned "10" to the value pointed by ptr, which is pointing to the address of X, so we have modified " X " indirectly.
In order that we can see that a pointer may take several different values during the same Program we have repeated the Process with Y and the same pointer.



Null Pointers:

NULL is a constant value defined in manifold C++ Libraries Including <iostream> Specially designed to indicate Null Pointers. In Case that this constant is not defined we can do it by our self by defining it to 0 (zero).



After a Pointer is declared, but before it has been assigned, it will contain an Arbitrary value. should we try to use the pointer prior to giving it a value, we will probably crash our Program. while there is no sure way to avoid using an uninitialized pointer, C++ Programmers have adopted a procedure that helps prevent some errors. By convention, if a pointer contains the Null ( Zero ) value, it is assumed to point to Nothing. Thus, if all unused pointers are given the Null value and we avoid the use of a Null pointer, we can avoid the accidental misuse of an uninitialized pointer. This is a good practice to follow.

Any type of pointer can be initialized to Null when it is declared. For Example, the following initializes ptr to Null:


int *ptr = 0; //Ptr is now a NULL Pointer; 

To check for a Null pointer, We can use an if-statement as shown in the given example.


if( ptr == NULL); //If Ptr is equal to NULL:
if( !ptr ); //If Ptr is NULL:
 
if( ptr != NULL); //If Ptr is not equal to NULL:
if( ptr ); //If Ptr is Not NULL:


It is always a good Practice to assign the pointer NULL to a pointer variable in case we don't have exact address to be assigned. This is done at the time of variable declaration. Consider the following Program:



/*Null Pointer : InfoBrother*/
 
#include<iostream> 
using namespace std;
 
main()
{
    int *ptr = NULL; //Pointer declaration: With NULL value:
    cout<<" The value of Ptr is : "<<ptr<<endl;
 
    return 0;
}


More About Pointers:

Pointer are the lifeline of Any C/C++ Programmer. The reason is not just the flexibility, but also the additional speed that a pointer adds to our program. It's the most Important topic in C++. Some C++ tasks are performed more easily with pointer, and other C++ tasks like Dynamic Memory Allocation and some advance C++ Function are Not possible without Pointers:










I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement