Advertisement

Pointers in c++ || Code with abuzar


Pointers in C++

Pointers is also a data type which is used the address int type . C++ is very easy to Learn and Fun  , Some C++ tasks are to performed more easily with pointers and other C++ tasks , such as dynamic memory allocation , cannot  be perform without them .

As we know that every variable is a memory location and every memory location has it's address define which can not be accessed using ampersand (&) operator which denotes an address in memory consider the following which will pint the addresses of the variable defined --

Syntax :

datatpe*var_name   ( Variable name)

int* ptr      // ptr points to the address which holds the  int data .


Example :

#include <iostreame>
using namespace std
int main (){
// Variable Declare 
int a=10;  
 //  Treat as a Pointer 
intb;     
 // b assign the address of a 
b=&a;     
Cout<<"Address of int data"<<&b<<endl; 
Cout<<"Value of int data"<<*b<<endl; 
Return 0;
}

Output :

 Address of int Data :  a0x61ff08

Value of Int Data : 10


What are Pointers ? 

A pointer is a variable whose value is the address of another variable . Like any Variable or constant , you must declare a pointer before you can work with it .

when you Create a variable in your C++ program , It is assigned some space the computer memory . The value of this variable is stored in the assigned location . to know the location in the computer memory where the data is stored in C++  provides the &(Reference) operator  The operator returns the address that a variable occupies.


Reference operators(&) and Deference Operator (*)

 The reference operator (&) return the variable address.

 The deference operator(*) helps us to get the value which is stored in memory address


For Example : 

 If you have a variable given the name num , stored in the address 0x234 and storing the value 28 .

 The reference operator (&) will return 0x234

The Deference operator (*) will return 5 .


Why Use Pointers in C/C++ 

Pointers can be used to pass of arrays and strings to function more efficiently . Pointers save the memory . Pointers reduce the length and Complexity of the Program . Pointers make possible to return more than one value from the function .


Pointers in C++ Video : Click Here

This is my YouTube channel : Click Here 
  
 Playlist of C++(Full Course) :Click Here

 Playlist of C++(Program ) : Click Here
 

 Please ! write comment if you find anything incorrect in this website .


 This article is contributed by Mohd abuzar . If you want to like code with abuzar or want to contribute with us so mail at - abuzar.abuzar.mohd325@gmail.com     
                                                     


                        






Post a Comment

0 Comments