cs201- introduction to programming- lecture 15
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 15 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 15Lecture 15
![Page 2: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/2.jpg)
In Today’s LectureIn Today’s Lecture
Pointers and Arrays Pointers and Arrays ManipulationsManipulations
Pointers ExpressionPointers Expression Pointers ArithmeticPointers Arithmetic Multidimensional ArraysMultidimensional Arrays Pointer String and ArrayPointer String and Array
![Page 3: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/3.jpg)
Pointers and Pointers and ArraysArrays
00
11
22
33
44
55
66
77
88
99
Starting Address of Array yint y [ 10 ] ; [0][1][2][3][4][5][6][7][8][9]
![Page 4: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/4.jpg)
The name of the array is like The name of the array is like aa
pointerpointer which contain the which contain the
address of the first element.address of the first element.
![Page 5: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/5.jpg)
Declaration of a Pointer Declaration of a Pointer VariableVariable
int y [ 10 ] ;int y [ 10 ] ;
int *yptr ;int *yptr ;
yptr = y ; yptr = y ;
yptr is a pointer to integer
![Page 6: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/6.jpg)
Declaration of a Pointer Declaration of a Pointer VariableVariable
00
11
22
33
44
55
66
77
88
99
y [ 3 ]
[0][1][2][3][4][5][6][7][8][9]
![Page 7: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/7.jpg)
int y [ 10 ] ;int y [ 10 ] ;
int *yptr ;int *yptr ;
yptr = y ;yptr = y ;
yptr ++ ;yptr ++ ;
![Page 8: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/8.jpg)
pointer variable yPtr
y[0] y[1] y[2] y[4]y[3]
3000 3004 3008 3012 3016
location
![Page 9: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/9.jpg)
In this case yptr is a pointer In this case yptr is a pointer
to integer so now when we to integer so now when we
increment yptr it points to increment yptr it points to
the next integerthe next integer
![Page 10: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/10.jpg)
Example 1Example 1#include<iostream.h>#include<iostream.h>
main ( ) main ( )
{{
int y [ 10 ] ;int y [ 10 ] ;
int *yptr = y ;int *yptr = y ;
yptr = y ;yptr = y ;
cout << yptr ;cout << yptr ;
yptr ++ ;yptr ++ ;
cout << yptr ;cout << yptr ;
}}
![Page 11: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/11.jpg)
yptr = y ; yptr = y ;
is same asis same as
yptr = &y [ 0 ] ;yptr = &y [ 0 ] ;……..……..
yptr = &y [ 2 ] ;yptr = &y [ 2 ] ;
![Page 12: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/12.jpg)
Example 2Example 2#include<iostream.h>#include<iostream.h>
main ( )main ( )
{{
int y [ 10 ] ;int y [ 10 ] ;
int *yptr ;int *yptr ;
yptr = y ;yptr = y ;
cout << yptr ;cout << yptr ;
yptr ++ ;yptr ++ ;
cout << *yptr ;cout << *yptr ;
}}
![Page 13: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/13.jpg)
main ( )main ( ){{
int x = 10 ;int x = 10 ;int *yptr ;int *yptr ;yptr = &x ;yptr = &x ;cout << yptr ;cout << yptr ;cout << *yptr ;cout << *yptr ;*yptr ++ ; *yptr ++ ;
}}
Example 3Example 3
increment whatever yptr points to
![Page 14: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/14.jpg)
Pointer Pointer ArithmeticArithmetic*yptr + 3 ;*yptr + 3 ;
cout << *yptr ;cout << *yptr ;
*yptr += 3 ;*yptr += 3 ;
This Is an Expression
![Page 15: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/15.jpg)
Pointer Pointer ArithmeticArithmetic
yptr = &x ;yptr = &x ;
yptr ++ ;yptr ++ ;
![Page 16: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/16.jpg)
Pointer Pointer ArithmeticArithmetic
int x =10 ;int x =10 ;
int *yptr ;int *yptr ;
yptr = &x ;yptr = &x ;
*yptr += 3 ; *yptr += 3 ;
yptr += 3 ;yptr += 3 ;
![Page 17: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/17.jpg)
Decrementing Decrementing
*yptr --*yptr --
![Page 18: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/18.jpg)
Pointer Pointer ArithmeticArithmetic
int *p1 ,*p2;int *p1 ,*p2;
……....
p1 + p2 ;p1 + p2 ;
Error
![Page 19: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/19.jpg)
Pointer Pointer ArithmeticArithmetic
int y [ 10 ] , *y1 , *y2 ; int y [ 10 ] , *y1 , *y2 ;
y1 = &y [ 0 ] ;y1 = &y [ 0 ] ;
y2 = &y [ 3 ] ;y2 = &y [ 3 ] ;
cout << y2 - y1 ; cout << y2 - y1 ;
![Page 20: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/20.jpg)
Pointer Pointer ArithmeticArithmetic
int y [ 10 ] ;int y [ 10 ] ;
int *yptr ;int *yptr ;
yptr = y [ 5 ] ;yptr = y [ 5 ] ;
cout << *( yptr + 5 ) ;cout << *( yptr + 5 ) ;
![Page 21: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/21.jpg)
Pointer Pointer ComparisonComparisonif ( y1 > y2 )if ( y1 > y2 )
if ( y1 >= y2 ) if ( y1 >= y2 )
if ( y1 == if ( y1 == y2 ) y2 )
![Page 22: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/22.jpg)
Pointer Pointer ComparisonComparison
if ( *y1 > *y2 )if ( *y1 > *y2 )
![Page 23: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/23.jpg)
ExampleExampleint y [ 10 ] ;int y [ 10 ] ;
int *yptr ;int *yptr ;
yptr = y ;yptr = y ;
cout << y [ 5 ] ;cout << y [ 5 ] ;
cout << ( yptr + 5 ) ;cout << ( yptr + 5 ) ;
cout << *( yptr + 5 ) ; cout << *( yptr + 5 ) ;
![Page 24: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/24.jpg)
ExampleExampleint que [ 10 ] ;int que [ 10 ] ;int y [ 10 ];int y [ 10 ];int *yptr ;int *yptr ;yptr = y ; yptr = y ; yptr = que ;yptr = que ;
![Page 25: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/25.jpg)
pointer variable vPtr
v[0] v[1] v[2] v[4]v[3]
3000 3004 3008 3012 3016
location
![Page 26: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/26.jpg)
StringsStrings
![Page 27: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/27.jpg)
String String InitializationInitialization
char name [ 20 ] ; char name [ 20 ] ;
name [ 0 ] = ‘A’ ;name [ 0 ] = ‘A’ ;
name [ 1 ] = ‘m’ ;name [ 1 ] = ‘m’ ;
name [ 2 ] = ‘i’ ;name [ 2 ] = ‘i’ ;
name [ 3 ] = ‘r’ ;name [ 3 ] = ‘r’ ;
name [ 4 ] = ‘\0’ ;name [ 4 ] = ‘\0’ ;
![Page 28: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/28.jpg)
String String InitializationInitialization
Strings is always Strings is always terminated with \0terminated with \0
![Page 29: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/29.jpg)
String String InitializationInitialization
char name [ 20 ] = “Amir” ;char name [ 20 ] = “Amir” ;
Array must be one character Array must be one character space larger than the number space larger than the number of printable character which of printable character which
are to be stored.are to be stored.
![Page 30: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/30.jpg)
Example 4Example 4char string1 [ 20 ] = “Amir”;char string1 [ 20 ] = “Amir”;char string2 [ 20 ] ;char string2 [ 20 ] ;char *ptrA, *ptrB ;char *ptrA, *ptrB ;prtA = string1 ;prtA = string1 ;prtB = string2 ;prtB = string2 ;while ( *ptrA != ‘\0’ )while ( *ptrA != ‘\0’ ){{
*ptrB++ = *ptrA++;*ptrB++ = *ptrA++;}}*ptrB = ‘\0’ ; *ptrB = ‘\0’ ;
![Page 31: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/31.jpg)
String Copy String Copy FunctionFunction
myStringCopy ( char *destination , const char myStringCopy ( char *destination , const char *source )*source )
{{
while ( *source != ‘\0’ )while ( *source != ‘\0’ )
{{
*destination++ = *source++ ;*destination++ = *source++ ;
}}
*destination = ‘\0’ ;*destination = ‘\0’ ;
}}
![Page 32: CS201- Introduction to Programming- Lecture 15](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e778b47959ad258b47fe/html5/thumbnails/32.jpg)
In Today’s In Today’s LectureLecture
Pointers and ArraysPointers and ArraysPointer ArithmeticPointer ArithmeticManipulation of ArraysManipulation of ArraysString ArraysString Arrays