Pointers C++ Slides
Pointers C++ Slides
• Pointers
• User defined Data types
• Structure
• Link List
• Object oriented programming
• Classes
• Constructors and Destructors
• Inheritance
• Friend functions
• File Handeling
Address or Reference Operator(&) /
Pointer or De-reference operator(*)
• In C and C++ & sign is used as reference or address
operator. When used with a variable as prefix, it return
the address of that variable.
Example
Int x; cout<<&x;
This operator is commonly used to pass a reference as
argument instead of a value to a function to update a
variable data.
• Asterisk symbol (*) is an Operator used to declare
pointer variable. It also return the value stored at
memory location to which a pointer is pointing.
Address or Reference Operator(&)
Passing a value and reference to a function
Void Disp(int *p, int arysize) void Disp(int *p, int arysize)
{ {
for(int i=0;i<arysize;i++) while(arysize-->=1)
{ {
cout<<*(p+i)<<endl; cout<<*p++<<endl;
} }
} }
main()
{
int array[5]={1,2,3,4,5},*p;
Disp(array,5);
system("pause");
}
Define a function factorial that can read data from
an array to a pointer and print factorial of each
elements of array
void factorial(int *p, int arysize)
{
long double fact;
while(arysize >=1) main()
{ { int array[5]={1,2,3,4,5},*p;
fact=1; factorial(array,5);
for(int n=*p;n>1;n--) system("pause");
{ }
fact=fact*n;
}
cout<<*p<<“!=“<<fact<<endl;
p++;
arysize--;
}
}
Pointers and Two Dimensional Arrays
int *p, mat[2][3]={{2,4,6},{8,10,12}};
The 2D array data is actually stored in 1D array format in memory.
2 4 6 8 10 12
p=mat[0] or p=&mat[0][0] or p=*mat assign the address of 0th element
to p.
*p or *(p+0+0) represent 0th element of array ie 2. where 0 0 represent
ith and jth indexes. ie *(p+i+j);