Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

POINTERS

• Pointers and arrays


• Pointers and Structures
• Pointers and functions(call by address)
Pointers- recap
int Quantity; //defines variable Quantity of type int
int* p; //defines p as a pointer to int
p = &Quantity; //assigns address of variable Quantity to pointer p

Now…
Quantity = 50;//assigns 50 to Quantity
*p = 50; //assigns 50 to Quantity

10/9/19 Department of CSE 2


Pointers and arrays
 When an array is declared, the compiler allocates a
base address and sufficient amount of storage to
contain all the elements of the array in contiguous
memory locations.

 The base address is the location of the first element


(index 0) of the array.

 The compiler also defines the array name as a


constant pointer to the first element.

10/9/19 Department of CSE 3


Pointers and arrays
An array x is declared as follows
int x[5] ={ 1,2,3,4,5};
Suppose the base address of x is 1000 and assuming
that each integer requires 2 bytes,
the five elements will be stored as follows

10/9/19 Department of CSE 4


Pointers and arrays
The name x is defined as a constant pointer pointing
to the first element, x[0] and therefore the value of x
is 1000, the location where x[0] is stored.
i.e. is x =&x[0] 1000;
If we declare p as an integer pointer, then we
can make the pointer p point to the array x by the
following statement.
p=x;
This is equivalent to X
p=&x;

p=&x[0];

10/9/19 Department of CSE 5


Pointers and arrays
Now we can access every value of x using p++ to move from one
element to another. The relationship between p and x is shown below.
p= &x[0] (=1000) (assuming int takes 2
bytes)
p+1=>&x[1] (=1002)
p+2=>&x[2] (=1004)
p+3=>&x[3] (=1006)
p+4=>&x[4] (=1008)
how address is calculated?
Address of x[3] = base address + (3 x scale factor of
data-type[integer])
=1000 +(3*2) = 1006

10/9/19 Department of CSE 6


Pointers and arrays
We can also use pointers to access array elements .
This method i.e. pointer accessing method is much
faster than array indexing.
*(p+3) gives the value of x[3].
Note:
If p1 and p2 are both pointers to same array, then
p2 – p1 can give the number of elements in the
array.

For e.g.; if p1= &a[0] and p2 = &a[5] then


p2-p1 = 5
10/9/19 Department of CSE 7
Pointers and arrays
void main()
{ Outpu
int arr[5] = { 31, 54, 77, 52, 93 }; t:
31
for(int j=0; j<5; j++) //for each
54
element, 77
cout << arr[j]; //print value; 52
} 93
The cout statement prints each array element in turn.
For instance, when j is 3, the expression arr[j] takes on
the value arr[3] and accesses the fourth array element,
the integer 52.
10/9/19 Department of CSE 8
Pointers and arrays
#include <iostream.h>
void main( )
Outpu
t:
{ 31
int arr[5] = { 31, 54, 77, 52, 93 }; 54
77
for(int j=0; j<5; j++) //for each element,
52
cout << *(arr+j); //print value 93
}
“arr” itself is a constant pointer which
can be used to access the elements.

10/9/19 Department of CSE 9


Pointers and arrays
// array accessed with pointer
#include <iostream.h> Outpu
void main() { t:
int arr[] = { 31, 54, 77, 52, 93 }; //array 31
54
int* ptr; //pointer to arr
77
ptr = arr; //points to arr 52
for(int j=0; j<5; j++) //for each element,
93
cout << *(ptr++); //print value
}
“ptr” is a pointer which can be used to access
the elements.

10/9/19 Department of CSE 10


Problems…
Write programs using pointers to
compute
1. Exchange two values (values stored in
variables x & y).
2. Sum of all elements stored in an
integer array.

10/9/19 Department of CSE 11


Exchanging two values
void main(){
int x, y, t,*a,*b;
a=&x; b=&y;
cin>>*a>>*b; // equivalent to
cin>>x>>y; Output:
t=*a; x= 10 & y
*a=*b; =5
*b=t;
cout<<“x=“<<x; x= 5 y =
10
cout<<“y=”<<y;
}
10/9/19 Department of CSE 12
Sum of all elements stored in an array

void main() {
int *p, sum=0, j, i=0;
int x[5] ={5, 9, 6,3,7};
p=x;
while(i<5){ Output:
sum+=*p;
Sum of
i++; elements =30
p++; }
cout<<“sum of elements =“<<sum;
}
10/9/19 Department of CSE 13
Dynamic array
#include <iostream>
#include <new>
using namespace std;
int main ()
{ int i,n; int * p;
cout << "How many numbers would you like to type? "; cin >> n;
p= new int[n];
for (i=0; i<n; i++)
{ cout << "Enter number: ";
cin >> p[i]; }
cout << "You have entered: ";
for (i=0; i<n; i++)
cout << p[i] << ", ";
delete[] p;
return 0; }
10/9/19 Department of CSE 14
How many numbers would you like to type? 5
Enter number : 75
Enter number : 436
Enter number : 1067
Enter number : 8
Enter number : 32
You have entered: 75, 436, 1067, 8, 32,

10/9/19 Department of CSE 15


Pointers as functions arguments
When we pass addresses to a function, the parameters receiving
the addresses should be pointers.

void change (int *);


void change void main( ) {
(int *p) int x = 20; Output:
{ change(&x); x after change:- 30

*p = *p + 10 ; cout<<“x after
} change:-”<< x;
}
10/9/19 Department of CSE 16
Function returning pointer
A function can return single value by its name or return multiple
values through pointer parameters.
int *larger (int *x,
int *larger (int *, int
*); int *y)
void main() {
{
if (*x > *y)
int a=10, b=20, *p;
return (x);
p=larger (&a, &b);
else
cout<<*p; return(y);
}
}
10/9/19 Department of CSE 18
Pointers and Structures- example
struct invent
{ void main()
char name[30];{
int number; struct invent prod[3], *ptr;
float price; for(ptr = prod; ptr < prod+3; ptr++)
}; cin>>ptr  name>>ptr  number>>ptr
 price;

ptr=prod;

while(ptr < prod+3)


{
cout<<ptr  name<<”\n”<<ptr
 number<<“\n”<<ptr  price;
ptr++;
}
10/9/19 } Department of CSE 19
WAP to swap the values of two variable using
Call by address.
Why the above problem can’t be solved using
Call by value

10/9/19 Department of CSE 20


Pointers and structures
Consider the following structure
struct inventory {
char name[30];
int number;
float price;
} product[2],*ptr;
This statement declares product as an array of 2 element, each of
the type struct inventory.
ptr=product; assigns the address of the zeroth element of product to
ptr
or ptr points to product[0];

10/9/19 Department of CSE 21


Pointers and Structures
Its members are accessed using the following notation
ptr name
ptr number
ptr price
The symbol  is called arrow operator (also known as member
selection operator)

When ptr is incremented by one, it points to the next record. i.e. product[1]

The member number can also be accessed using


(*ptr).number
Parentheses is required because ‘.’ has higher precedence than the operator *

10/9/19 Department of CSE 22

You might also like