You are on page 1of 14

Pointers

int x=4,y=7

Pointers
949
950
3000 950 px 951 4 x
952
7 y
3002 952 py 953

© Dr J.Iqbal © Dr J.Iqbal

POINTERS Reasons to use Pointers.


• It provides a way of accessing a variable without • To return more than one value from the function.
referring to the variable directly. The mechanism
used for this is the address of the variable. • To pass arrays and strings more conveniently from
one function to another.
• Program statement can refer to the variable
indirectly, using the address of the variable. • To manipulate arrays more easily by moving
pointers to them.

© Dr J.Iqbal © Dr J.Iqbal

1
Reasons to use Pointers. Lvalue and Rvalue
• To create complex data structures, such as linked • The lvalue of a variable is the address in memory
lists and binary trees, where one data structure must where that variable is stored.
contain reference to other data structure. • The rvalue of a variable is the data that a variable
holds, in other words, it is the value of the variable.
• To communicate information about memory, as in
the function malloc( ), which returns the location of int count = 5;
free memory by using pointer.
• Here rvalue of count is 5. lvalue of count is the
address.
• Once variable is define its lvalue is fixed and cannot
be changed. Rvlaue can be changed.

© Dr J.Iqbal © Dr J.Iqbal

Lvalue and Rvalue Format of Pointer Declaration.


int x=4,y=7
Lvalue is a variable and an Rvalue is a constant. 949
950
3000
950 px 951 4 x
952
3002 952 953 7 y
e.g var = 3; p
y

Expression that can appear on the left side of an


assignment statement is a variable and is called an
Names of
“lvalue”. An expression that must remain on the right
pointer
side of the equal sign because it is a constant is called int * px, * py ; variables.
“rvalue”.

Indicates variable is a
Indicates that pointer (i.e., that it will hold
pointers will point
an address)
to variables of
type int.
© Dr J.Iqbal © Dr J.Iqbal

2
THE INDIRECTION OPERATOR Pointer Constants and Pointer Variables
• A pointer constant is an address (lvalue of some other
• The asterisk (*), when used in pointer definition constant).
means “pointer data type”, just as int means
“integer data type”.
• A pointer variable is a place to store address.
• Apart from definition, it means “variable
pointed to by”.

*px = 3;

It means “assign the variable pointed to by px


the value of 3”
© Dr J.Iqbal © Dr J.Iqbal

Pointer Constants and Pointer Variables Pointer Constants and Pointer Variables
int x=4,y=7
int *px= &x
int *py=&y
A pointer constant is an address and a pointer
949 variable is a place to store address.
950
3000 950 px 4 x
951
952 int *px,*py;
3002 952 py 7 y
953

1310 and 1310


1312 are
px px and py are
px and py are pointer 950 and 952 are pointer 1312
variables (space in pointer constants constants pointer variables
py
memory which can (addresses of data (addresses of (space in memory
hold any address). items). data items). which can hold
any address).
© Dr J.Iqbal © Dr J.Iqbal

3
Lvalue and Rvalue Address and Data
int *px= &x int x=4,y=7
int *py=&y int x=3;
949
950
int *ptr; // int *ptr=&x;
3000 950 951 4 x
px ptr=&x;
952
7 y
3002 952 py 953
x = data // 3
&x = Address
px=&x x=4 ptr = address
*ptr = data
lvalue=3000 lvalue=950

rvalue=950 rvalue=4
*px== x ==4
© Dr J.Iqbal © Dr J.Iqbal

int x=4,y=7 px=&x;


Use of Pointers py=&y; *px=*px+10;
*py=*py+10;
949
950 949
951 4 x
void main ( ) 950 x
952 14
{ 951
7 y
953 952
int x = 4, y = 7; 17 y
953
int *px, *py;
px = &x; int *px=&x;
py = &y; int *py=&y;

cout << “x = ” << x<<“y =”<<y; 950 px 950 px


*px = *px + 10;
*py = *py + 10; 952 py 952 py
cout << “x = ” << x<<“y =”<<y;
}
Operation of the addition using pointers
© Dr J.Iqbal © Dr J.Iqbal

4
Legal and illegal Equivalents
void main (void)
{ int a;
int *ptr1; void main()
{ char c,a=10;
char c;
char *ptr2; char *p1=&a;
int fred;
c=*p1; //equivalent the expreesion "c=a"
ptr2=&c; // ok. Char pointer init with add of a char
ptr1=&fred; // ok. int pointer init with the address of an int *p1=*p1**p1; //equivalent the expreesion "a=a*a"
ptr1=&a ; // ok. int pointer init with the address of an int (*p1)++; //equivalent the expreesion "a++"
ptr2=&fred; // NO. cannot assign the address of an int to a char
ptr2=&'#'; // NO. cannot take the address of an implicit constant c=*&a; //equivalent the expreesion "c=a"
ptr2=&25; // NO. cannot take the address of an implicit constant
ptr2=&(a +3); // NO. cannot take the address of an expression
} }

© Dr J.Iqbal © Dr J.Iqbal

PASSING DATA TO A FUNCTION Passing Values to a Function


/* tests function which accepts two values */
void gets2 (int, int); /*prototype*/
There are two ways to pass data to a function. void main ( )
• Pass by value {
• Pass by reference int x = 4, y = 7; /*initialize variables*/
gets2( x, y ); /*pass vars to function*/
}

void gets2 ( int xx, int yy )


{ cout <<“First is =”<< xx<<“Second is =”<<yy;
}

© Dr J.Iqbal © Dr J.Iqbal

5
void main(){
Passing Addresses to a Function
int x=4, y=7; void gets( int xx, yy){ /* test function that returns two values */
gets( x,y); cout <<xx<<“ ”<<yy;
void rets2 (int *, int *); /*prototype*/
} }
void main ( ){
int x, y; /* variables*/
rets ( &x, &y ); /*get values from function*/
cout <<“First is =”<< x<<“Second is =”<<y;
x 4 xx }
4

7 y 7 yy /* returns two numbers */


void rets2 ( int *px, int *py )
{ *px = 3; /* set contents of px to 3 */
*py = 5; /* set contents of px to 5 */
Values are passed to function and duplicated in the }
function’s memory space.
© Dr J.Iqbal © Dr J.Iqbal

Passing Values to Pointer Variables Passing Values to Pointer Variables


void main(){ void call1( int *px, *py){ void main(){ void call1( int *px, *py){
int x, y; *px = 3; int x, y; *px = 3;
call1( &x, &y); *py = 5; call1( &x, &y); *py = 5;
} } } }

addresses addresses

1310 3 x 1310 px 1310 3 x 1310 px

1312 5 y 1312 py 1312 5 y 1312 py

© Dr J.Iqbal © Dr J.Iqbal

6
Pointers and Arrays
/* Using pointers to print out values from array */
void main ( )
{
Pointers and Arrays static int nums [5] = { 92, 81, 70, 69, 58 };
int dex ;

for ( dex = 0; dex <5; dex++ )


cout << *(nums + dex)<<“ ”;
}
Address of num0
© Dr J.Iqbal © Dr J.Iqbal

Here,
* ( nums + dex ) is same as nums [ dex ].
Initializing Pointers to Arrays
/* Initializing Pointers */
1390 int * ptr; /* pointer to int (or int array) */
nums 1400 92 char *chptr; /*pointer to char(or char array)*/
1401 nums[0]
nums+1 1402 81 int table[3]= {5, 6, 7}; /* array */
1403 nums[1]
nums+2 1404 70
1405 nums[2] ptr = table; /*assign array address to pointer*/
nums+3 1406 69
1407 nums[3]
nums+4 1408 58 cout << *ptr; /*print first element of array*/
1409 nums[4]
nums+3= 1400+3*2=1406 cout << *(ptr+1); /* print second element of array */
number of bytes
per integer.
Scalar value
© Dr J.Iqbal © Dr J.Iqbal

7
Write out put in your copy
Use of Pointers To Arrays In Function
/* addarray.cpp*/
#define size 5
Consider the example on the next slide to see how void addcon ( int *, int, int );
OUTPUT OF THE PROGRAM
void main ( )
a function can use pointers to access elements of {
an array, whose address is passed to the function static int array [size] = { 3, 5, 7, 9, 11}; 13 16 19 22 25
int konst = 10, j;
as an argument. addcon ( array, size, konst );
for ( j=0; j < size; j++ )
cout << * (array + j) << “ ”;
}
void addcon ( int *ptr, int size, int con )
{
int k;
for ( k = 0; k < size; k++ )
* (ptr + k) = *( ptr + k) + con + k;
}
© Dr J.Iqbal © Dr J.Iqbal

POINTERS AND STRINGS


Strings Initialized as Pointers 1400 salute
1400 G
void main ( ) 1401 r
{ 1402 e
char *salute = “ Greetings ” ; 1403 e
1404 t
char name [ 81] ; i
1405
puts ( “Enter your name: ” ); 1406 n
1407 g
gets ( name );
1408 s
puts ( salute );
1409 \0
puts ( name );
} char *salute=
“Greetings”;
© Dr J.Iqbal © Dr J.Iqbal

8
INITIALIZING AN ARRAY OF POINTERS TO
STRINGS
static char *list[ 5 ]= 1000 K a t r i n a \0
1008 N i g e l \0
{ “Katrina”, A l i s t a i r \0
1013
”Nigel”, 1022 F r n c s c a \0
1032 G u s t a v \0
”Alistair”,
”Frncsca”,
”Gustav” }; 1038
1000 List[0]
Initializing a group of strings using pointer 1008 List[1]
notation uses less memory than initializing 1013 List[2]
1022 List[3]
them as a two-dimensional array. 1032 List[4]

Array of pointers to strings

© Dr J.Iqbal © Dr J.Iqbal

Parts of Arrays are Arrays


We can think of a two dimensional array as an 1000 K a t r i n a \0
1008 N i g e l \0
array of arrays. In this case the definition A l i s t a i r \0
1013
1022 F r n c s c a \0
1032 G u s t a v \0
static char list [MAX] [LEN];
1038
can be thought of as setting up a one- 1000 List[0]
dimensional array of MAX elements, each of 1008 List[1]
1013 List[2]
which is a one-dimensional array LEN 1022 List[3]
characters long. 1032 List[4]

Array of pointers to strings

© Dr J.Iqbal © Dr J.Iqbal

9
#define MAX 30
Manipulating Pointers to Strings #define LEN 81
Void main ()
This program accepts a series of names typed in by a { static char name [MAX] [LEN];
user, places them in an array, and sorts the pointers to char *ptr[MAX];
char *temp;
the array so that, in effect, the name are arranged into
int cnt=0;
alphabetical order. int in, ot;
while ( cnt < MAX)
{ cout << “Name =”<< cnt+1;
gets (name[cnt]);
if (strlen(name[cnt])==0)
break;
ptr [cnt++]=name[cnt];
}

© Dr J.Iqbal © Dr J.Iqbal

for ( ot=0; ot < cnt-1; ot++ ) Sorted Arrays


for ( in= ot+1; in<cnt, in ++ )
1000 K a t r i n a \0
if ( strcmp (ptr[ot], ptr[in]) >0 ) N i g e l \0
1008
{ temp = ptr [in]; 1013 A l i s t a i r \0
1022 F r n c s c a \0
ptr [in] = ptr [ot]; G u s t a v \0
1032
ptr [ot] = temp;
} 1038
1000 List[0]
cout << “SORTED LIST”; 1008 List[1]
for (ot=0; ot < cnt; ot ++) 1013 List[2]
1022 List[3]
cout << “Name ::” << ot+1 <<“ “ << ptr[ot]; 1032 List[4]
} Array of pointers to strings

© Dr J.Iqbal © Dr J.Iqbal

10
Sorted Arrays
Two Dim Array
1000 K a t r i n a \0 • b[5] == *(b+5)
1008 N i g e l \0
1013 A l i s t a i r \0
F r n c s c a \0 • int a[5][5];
1022
1032 G u s t a v \0 • a[2][2] == *(*(a+2)+2)

1038 Address of 2D array


1013 1000 List[0]
1022 1008 • &a[0][0] == &a[0] == a[0]== a+0 = = *(a+0)
List[1]
1032 1013 List[2] • &a[1][0] == &a[1] == a[1]== a+1 = = *(a+1)
1000 1022 List[3] • &a[1][1] = = *(a+1)+1
1008 1032 List[4]

Array of pointers to strings • What is the difference between a+1 and *(a+1)

© Dr J.Iqbal © Dr J.Iqbal

Equivalent Expressions
Difference between a+1 and *(a+1)
Mixed Notation
• a+1 == *(a+1)
int a[n][m];
• a+1+1 != *(a+1)+1
• a+2 != *(a+1)+1
1. a[n][m]
• a+2 means you have moved to next row
2. *(*(a+n)+m)
• *(a+1)+1 means move to next col in same row
3. *(a[n]+m)
4. (*(a+n))[m]
• Array_ex.cpp

© Dr J.Iqbal © Dr J.Iqbal

11
Pointers to Pointers Uses of pointer to pointer
• int k=4,*ptr, **ptoptr; • Creation of dynamically sizeable arrays
• Use of malloc() and reallloc()
• ptr=&k
• ptoptr=&ptr;

© Dr J.Iqbal © Dr J.Iqbal

K=4; ptr=&k;
Lvalue and Rvalue
949
950 • The lvalue of a variable is the address in memory
4 k 3000 950 ptr
951 where that variable is stored.
952
953 • The rvalue of a variable is the data that a variable
holds, in other words, it is the value of the variable.
ptoptr=&ptr
int count = 5;
• int k=4,*ptr, **ptoptr;
4000 ptoptr
• Here rvalue of count is 5. lvalue of count is the
3000
address.
• ptr=&k
• ptoptr=&ptr;
• Once variable is define its lvalue is fixed and cannot
be changed. Rvlaue can be changed.

© Dr J.Iqbal © Dr J.Iqbal

12
Lvalue and Rvalue
int **ppx= &px int *px= &x int **ppx= &px int *px= &x
int x=4,y=7 int x=4,y=7
int **ppy=&py int **ppy=&py
int *py=&y int *py=&y
949 949
950 950
9000 3000 ppx 3000 950 951 4 x 9000 3000 ppx 3000 950 951 4 x
px px
952 952
ppy 953 7 y ppy 953 7 y
9002 3002 3002 952 py 9002 3002 3002 952 py

Address Data
ppx=3000 &px=3000 &x=950
*ppx= 950 px=950 x= 4
int **ppx=&px int *px=&x int x=4 **ppx=4 *px= 4

lvalue=9000 lvalue=3000 lvalue=950

rvalue=3000 rvalue=950 rvalue=4


© Dr J.Iqbal © Dr J.Iqbal

int **ppx= &px


int **ppy=&py
int *px= &x int x=4,y=7 Write output in your copy
int *py=&y
949 void main (void)
950 {
9000 3000 ppx 3000 950 951 4 x
px
952 int x[8] = {10, 20, 30, 40, 50, 60, 70 , 80};
ppy 953 7 y float table [2][3] = { {1.1, 1.2, 1.3}, {2.1, 2.2,2.3} };
9002 3002 3002 952 py
char * color[6] = { "red", "green", "blue", "white", "black", "yellow" };

cout << "1. "<< x[2];


cout << "\n2. "<< *(table +1);
ppx=3000
px=950 &x=950 cout << "\n3. "<< *(*(table+1)+1);
*ppx= 950
*px= 4 x= 4 cout << "\n4. "<< *(color + 2);
**ppx=4
cout << "\n5. "<< ( *x+2 );
cout << "\n6. "<< *(x+2);
int **ppx=&px int *px=&x int x=4 cout << "\n7. "<< *(*(table));
cout << "\n8. "<< *(*(table)+1)+1;
cout << "\n9. "<< *(color+1);
lvalue=9000 lvalue=3000 lvalue=950 cout << "\n10."<< *(table+1)+1;

}
rvalue=3000 rvalue=950 rvalue=4
© Dr J.Iqbal © Dr J.Iqbal

13
K=4; ptr=&k; K=4; ptr=&k;
949 949
950 3000 950 3000
951 4 k 950 ptr 951 4 k 950 ptr
952 952
953 953

ptoptr=&ptr ptoptr=&ptr
• int k=4,*ptr, **ptoptr; • int k=4,*ptr, **ptoptr;
4000 3000 ptoptr 4000 3000 ptoptr
• ptr=&k • ptr=&k
• ptoptr=&ptr; • ptoptr=&ptr;

&k= k= &ptoptr=
k= 4 &k=950

*ptr= &ptr= ptr= *ptr=4 ptr=950 &ptr=3000

*ptoptr= **ptoptr= ptoptr= **ptoptr=4 *ptoptr=950 ptoptr=3000 &ptoptr=4000


© Dr J.Iqbal © Dr J.Iqbal

14

You might also like