Professional Documents
Culture Documents
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
© 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
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;
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
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
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
© 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
addresses addresses
© 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 ;
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
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]
© Dr J.Iqbal © Dr J.Iqbal
© 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
© 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)
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
}
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
14