C++ Pointer

I tried to clear here my logic for pointer basic

#include<iostream>
using namespace std;

int main(){

int zaki_live;
int *point_koro;
int show_koro;

zaki_live=50;
point_koro=&zaki_live;
show_koro=*point_koro;

//now printing the output

cout<<"Value of zaki_live :"<<zaki_live<<endl;
cout<<"Value of point_koro :"<<*point_koro<<endl;
cout<<"Value of show_koro_koro :"<<show_koro<<endl;


return 0;}

 

#include<iostream>
using namespace std;

int main(){


int var1;
int *var_show;
int var_output;

var1=20;
var_show=&var1;
var_output=*var_show;

//now printing the output

cout<<"Value of var1 :"<<var1<<endl;
cout<<"Value of *var_show :"<<var_show<<endl;
cout<<"Value of var_output :"<<var_output<<endl;


return 0;}

Actually in c++ pointer can be declare like this

type  *var_name;

example:

int *ip;
double *dp;
float *fp;
char *ch;

 

#include<iostream>
using namespace std;

int main(){
int variable=20; //actual variable declaration
int *ip; //pointer variable
ip=&variable; //store the adddress of the variable in pointer varaibel


cout<<"value of the variable: ";
cout<<variable<<endl;

//print the address stored in pointer variable
cout<<"address stored in pointer :";
cout<<ip<<endl;

cout<<"address of the variable :";
cout<<&variable<<endl;
//access the value of the address through the pointer
cout<<"Value of ip *varibale: ";
cout<<*ip<<endl;
return 0;
}

C++ NULL Pointers

It actually represent no where

int *zaki=0;
int *dhaka=NULL;

Both the examples meaning the same attitude of the c++

It actually default by the operating systems

Here is one example:

#include<iostream>
using namespace std;

int main()
{
    int *ptr=NULL;
    
    cout<<"The value of ptr is: "<<ptr;

    return 0;
}

We can also define by this

if(ptr) //it will proceed if p is not null
and
if(!ptr) //it will proceed if p is null

Pointer arithmetic

Incrementing a pointer:

#include<iostream>
using namespace std;
const int MAX=3;
int main()
{
int var[MAX]={10,100,200};
int *ptr;

ptr=var; //address of vcar in pointer ptr
for(int i=0;i<MAX;i++)
{
    cout<<"Address of var["<<i<<"] = ";
    cout<<ptr<<endl;

    cout<<"Value of var["<<i<<"] = ";
    cout<<*ptr<<endl;

ptr++;

}



    return 0;
}

Decrementing a pointer:

#include<iostream>
using namespace std;
const int MAX=3;
int main()
{
int var[MAX]={10,100,200};
int *ptr;

ptr=&var[MAX-1];
for(int i=MAX;i>0;i--){
cout<<"Address of var["<<i<<"] = ";
cout<<ptr<<endl;

cout<<"Value of var["<<i<<"] = ";
cout<<*ptr<<endl;

//point to the previous location
ptr--;

}
    return 0;
}

Comparison with pointer:

 

#include<iostream>
using  namespace std;
const int MAX=3;

int main(){
int var[MAX]={10,100,200};
int *ptr;
//let us have address of the first element in pointer
ptr=var;
int i=0;

while(ptr<=&var[MAX-1]){
cout<<"Address of var["<<i<<"]: ";
cout<<ptr<<endl;

cout<<"Value of var["<<i<<"]: ";
cout<<*ptr<<endl;

//point to previous location
ptr++;
i++;
}

return 0;
}

C++ pointer vs arrays:

#include<iostream>
using namespace std;
const int MAX=3;

int main(){

int var[MAX]={10,100,200};
for(int i=0;i<MAX;i++)
{
*var=i;
cout<<var<<endl;
}

return 0;
}

C++ Array of pointers

#include<iostream>
using namespace std;
const int MAX=3;
int main(){

int var[MAX]={10,100,200};
int *ptr[MAX];
for(int i=0;i<MAX;i++){
    ptr[i]=&var[i];
}

for(int i=0;i<MAX;i++)
{
    cout<<"Value of var["<<i<<"] = ";
    cout<<*ptr[i]<<endl;

}
return 0;
}

and also can store data like this with pointer and array mutually

#include<iostream>
using namespace std;
const int MAX=4;
int main()
{

char *names[MAX]={"Zara Ali","Mitha Ali","Sara Ali","Zaki Live"};

for(int i=0;i<MAX;i++)
{
    cout<<"Value of names["<<i<<"] = ";
    cout<<names[i]<<endl;

}
return 0;
}

C++ Pointer to Pointer:

#include<iostream>
using namespace std;
int main()
{
int var;
int *ptr;
int **pptr;

var=300;

ptr=&var; //take the address of teh var

pptr=&ptr;//take the address of ptr using the address operator &

//print

cout<<"Value of var: "<<var<<endl;
cout<<"Value available at *ptr: "<<*ptr<<endl;
cout<<"Value available at **pptr:"<<**pptr<<endl;
return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *