Showing posts with label pointer. Show all posts
Showing posts with label pointer. Show all posts

Thursday, 19 January 2012

Program Of This Pointer In C++


#include<iostream.h>
#include<conio.h>
class add
{
private:
int a,b;
public:
void getdata(int a,int b)
{
 this->a=a;
 this->b=b;
}
void display()
{
int c;
c=a+b;
cout<<"\n NAswer="<<c;
cout<<"\n this=="<<this;
}

}
main()
{
clrscr();
int a,b;
add a1,a2;
cout<<"enter first no.";
cin>>a;
cout<<"enter second no.";
cin>>b;
a1.getdata(a,b);
a1.display();
a2.getdata(6,3);
a2.display();
getch();
}


------------------------------------------------------------------------------------------------------------------------------------------------------------



It is a misbelief that the 'this' pointer is a hidden member of a class or struct. It is a hidden parameter of non-static member functions. When you declare a function the compiler adds an extra parameter to function's prototype. The type of the parameter depends on how the function is declared.



Program Of 2D Array of Pointer In C++


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int i,j,a[3][3],*ptr;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"Enter NUmber=";
cin>>a[i][j];
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
ptr = &a[i][j];
cout<<"\n Value of a["<<i<<"]["<<j<<"]==>"<<a[i][j]<<" And Address of a["<<i<<"]["<<j<<"]==>"<<ptr;
}
cout<<"\n";

}
getch();
}

Program Of Pointer In C


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c;
int *ptr1,*ptr2,*ptr3;
a=10;
b=15;
c=45;
ptr1=&a;
ptr2=&b;
ptr3=&c;
printf("\n Value of a= %d",a);
printf("\n value of b= %d",b);
printf("\n value of c= %d",b);
printf("\n  Addrss of a= %u",ptr1);
printf("\n Address of b= %u",ptr2);
printf("\n Address of c= %u",ptr3);
getch();
}

--------------------------------------------------------------------------------------

Pointer is a variable that stores a memory address.

 OK, that is simple ! But, what is a memory address then?

 Every variable is located under unique location within a

 computer's memory and this unique location has its

 own unique address, the memory address. Normally,

 variables hold values such as 5 or “hello” and these 

values are stored under specific location within 

computer memory. However, pointer is a different beast,

 because it holds the memory address as its value and

 has an ability to “point” ( hence pointer ) to certain value 

within a memory, by use of its associated memory 

address.