0% found this document useful (0 votes)
22 views

PointersT c++

Uploaded by

e716580009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

PointersT c++

Uploaded by

e716580009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

UNIT3 :

Introduction to Pointers in C+++


Adjectives of this unit
- What is poiners ?
- Declaration and Initialization pointer .
- Eamples .
- Pointers using functions .
- Array and pointers .

T.SUB: Abdulmalik A Alsarori


Pointers
 Pointer is a variable that stores the memory address of
an object.
 Special type of variables which hold the address of
another variable i.e no values or datas are stored but it
points to another variable where data is stored.
Declaration and Initialization pointer
 Syntax of declaration :
datatype *variable_ name;
datatype * variable_ name;
datatype * variable_ name;

• Initialization of pointer
datatype *varable_name=& other variable // referencing

• Example :
int *x = &y ;
int* x = &y ;
int * x = & y ;
Declaration and Initialization pointer
point to another pointer
 Pointer : can be used to point to another
pointer also i.e it can store the address of
another pointer.
Differences between & and *
 & is the reference operator and can be read
as "address of“.
 * is the dereference operator and can be
read as "value pointed by“
All expressions below are true:
• x= 25;
• y = &x;
• x == 25 // true
• &x == 1776 // true
• y == 1776 // true
• *y == 25 // true
Examples
 #include <iostream.h>
int main ()
{
int var1;
char var2;
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0; Output
} Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6
Examples
#include <iostream.h>
int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0; Output
} Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Examples
 #include<iosream.h>
int main()
{
int a=100;
int *p1;
int * *p2;
p1=&a; //p1 points to address of a
p2=&p1; // p2 points to address of p1
cout<<“address of a”<<&a;
cout<<“address of a”<<p1;
cout<<“value of a”<< *p1;
cout<<“value of a”<< * *p2;
cout<<p2;
cout<< *p2;
}
Reference variable in C++
 When a variable is declared as reference, it becomes an
alternative name
for an existing variable.
 A variable can be declared as reference by
putting „&‟ in the declaration.
Program using reference variable
 #include<iostream.h> Output

int main() 100


100
{ 0012dcD2
int a=100; 0012dcD2
int &ref=a;
cout<<“value of a is”<<a;
cout<<“value of ref is”<<ref;
cout<<address of a is”<<&a;
cout<<address of a is”<<&ref;
}
Both “a” and “ref ” are used for
same memory location as
alternative name
pointers using function
 A function can be invoked in two manners :
- call by value .
- call by reference .
 The call by value method copies the value of actual
parameters into formal parameters i.e the function
creates its own copy of arguments and uses them.
 Call by reference In call by reference method in
place of calling a value to the function being called ,
a reference to the original variable is passed .i.e the
same variable value can be accessed by any of the
two names.
Call by value
Values of
variables a
and b are
passed to X,Y
add(a,b);
}
void add(int x,int y); Now if you
change
Call by value { the value of X
where the values
of
--------; and Y, those
variable are } changes are
passed not seen in a and
to functions b
program to illustrate the concept of call by value

#include<iostream.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
return 0;
}
void add(int x,int y)
{
int c;
c=x+y;
cout<<“addition is”<<c;
}
Call by reference

In function call
We write reference
variable for formal
arguments
add(a,b);
}
void add(int &x,int &y);
{
--------;
} &X,&Y will be the
No need for reference variable for
return a and b. if we change
statement X and Y,Value of a
and b are changed
accordingly.3
Program to illustrate call by reference

#include<iostream.h>
#include<conio.h> void swap(int &X,&Y)
void swap(int &,int &); {
int main() int temp;
{ temp=X;
int a,b;
cout<<“enter the values of a and b”; X=Y;
cin>>a>>b; Y=X;
cout<<“before swaping”; }
cout<<“A”<<a;
cout<<“B”<<b;
swap(a,b);
cout<<“after swaping”;
cout<<“A”<<a;
cout<<“B”<<b; }
Array and pointers

 int array[] = {45,46,47};


 we can call the first element in the array
by saying: *array or array[0].
 Also the second element would be call:
*(array +1) or array[1]
Array of character pointer
Example

char *p1 = &str1[0],


char *p2 = &str2[0];
while(I) {
if(*p1 != *p2) return *p1 - *p2;
If(*p1 == '\0' || *p2 == '\0')
return 0; p1++; p2++;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy