Friday, April 24, 2020

Pointer in C++


Pointer in C++:


pointer in c++ programming
pointer in c++

What is pointer??


Pointers in c++ are variables that store integer values but those integers are the memory address of the other variables. A pointer that stores the address of any variable suppose we say that it stores the address of “x” then that pointer is said to pointing toward that “x”. 

When we declare a variable, the computer relates that variables name with a specific location in the memory and then stores that value there.

 Now whenever you refer to the variable by name in your code, the computer will always follow these two steps:

  1.               First of all, lookup the address that the variable name corresponds to
  2.        Then it will go to that location in memory and retrieve or set the value already stored in it.


Syntax:


The syntax of the pointer is the following:

data type * pointer_name;

Here we can use any type of data.


Initialization of pointer:


We can create a pointer exactly the same way;

int* a;

 Just Like every other object, we have to initialize a pointer before we can use it.

we can create a pointer to any type of object. The pointers take up 4 bytes and store a single numerical value.

we know that if we create an int without assigning it a value, it'll be just some garbage value. Whatever is value is there in the memory at the time.



Declaration of Pointer:


 If we want to declare a pointer variable named ptr, that will point to an integer variable of the name x:

 int * ptr = &x;

Here “int *ptr” creates a pointer to an integer value, which we initialize to the address of x.

Dereferencing of a Pointer:


There is something special that a pointer can do and that is we get back whatever object is in the memory location of that single numerical value. It means if my pointer stores the value of any location, when I dereference the pointer, I get back to that memory location and whatever is at that memory location will be retrieved.


How we know what numerical value to put into a pointer???

 So here is the answer, there exists an operator "&" which will return the address of any object (i.e. the value at that place in memory where that object is).
Int a; //any integer value
&a;// the address of that integar


Example:


A simple program to display the address of a pointer.

Source Code:

 #include<iostream>
 using namespace std;
 int main ()
{
int vari1;
 int vair2;
 cout << "address of vari1 is: ";
 cout << &var1 << endl;
 cout << "address of vari2 is: ";
 cout << &var2 << endl;
 return 0;
 }

Output:
address of vari1:
0xbfebd5c0
address of vari2:
0xbfebd5b6

Pointer Arithmetic:


Pointer arithmetic is the arithmetic of adding (and subtracting) values from a pointer. A pointer holds a memory address, so pointer arithmetic is the arithmetic of adding (and subtracting) values from a memory address.   
   
 If we are adding the value 1 to a pointer, it doesn’t mean that
 "We are adding 1 to that memory address we're holding".

We mean

 "To add to your memory to point at the next memory address along".
Now, let's add 1 to the pointer:


P=p+1; // now the pointer have the value of the next int.

Incrementing a Pointer:


The following program increments the variable pointer to access each succeeding element of the array :

Source code:


c++-program-for-increment-operator


Output:


program to display increment pointer in c++ programming


Decrementing a Pointer:

 A simple program to display the decrement of pointer.


Source code:

c++-program-for-decrement pointer


OUTPUT:



program to display decrement pointer in c++ programming


Pointer Comparisons:

A simple program to display a comparison of pointer.


Source code:


comparison-pointer-c++-program


OUTPUT:


program to display pointer comparison in c++ programming


Why we need pointers??


Pointers are crucial to the processing of strings. They are also necessary when we are making linked lists and using dynamic memory allocation. Using pointers for dynamic memory allocation means:



- We can use the same pointer for multiple memory locations.
- Assign this value to any other pointer with the same type or a void.




Thank You  for reading the article






No comments:

Post a Comment

If else in c

IF Else in C: If-else in c: If else is the most used condition in our life. Whenever we need to make any decision we use...