+ All Categories
Home > Education > CS201- Introduction to Programming- Lecture 15

CS201- Introduction to Programming- Lecture 15

Date post: 18-Dec-2014
Category:
Upload: bilal-ahmed
View: 14 times
Download: 1 times
Share this document with a friend
Description:
Virtual University Course CS201- Introduction to Programming Lecture No 15 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Popular Tags:
32
Introduction to Introduction to Programming Programming Lecture 15 Lecture 15
Transcript
Page 1: CS201- Introduction to Programming- Lecture 15

Introduction to Introduction to ProgrammingProgramming

Lecture 15Lecture 15

Page 2: CS201- Introduction to Programming- Lecture 15

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

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

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

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

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

int y [ 10 ] ;int y [ 10 ] ;

int *yptr ;int *yptr ;

yptr = y ;yptr = y ;

yptr ++ ;yptr ++ ;

Page 8: CS201- Introduction to Programming- Lecture 15

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

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

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

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

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

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

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

Pointer Pointer ArithmeticArithmetic

yptr = &x ;yptr = &x ;

yptr ++ ;yptr ++ ;

Page 16: CS201- Introduction to Programming- Lecture 15

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

Decrementing Decrementing

*yptr --*yptr --

Page 18: CS201- Introduction to Programming- Lecture 15

Pointer Pointer ArithmeticArithmetic

int *p1 ,*p2;int *p1 ,*p2;

……....

p1 + p2 ;p1 + p2 ;

Error

Page 19: CS201- Introduction to Programming- Lecture 15

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

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

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

Pointer Pointer ComparisonComparison

if ( *y1 > *y2 )if ( *y1 > *y2 )

Page 23: CS201- Introduction to Programming- Lecture 15

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

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

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

StringsStrings

Page 27: CS201- Introduction to Programming- Lecture 15

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

String String InitializationInitialization

Strings is always Strings is always terminated with \0terminated with \0

Page 29: CS201- Introduction to Programming- Lecture 15

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

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

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

In Today’s In Today’s LectureLecture

Pointers and ArraysPointers and ArraysPointer ArithmeticPointer ArithmeticManipulation of ArraysManipulation of ArraysString ArraysString Arrays


Recommended