introduction to programming lecture 15. in today’s lecture pointers and arrays manipulations...

32
Introduction to Introduction to Programming Programming Lecture 15 Lecture 15

Upload: shyanne-leavey

Post on 14-Dec-2015

255 views

Category:

Documents


11 download

TRANSCRIPT

Introduction to Introduction to ProgrammingProgramming

Lecture 15Lecture 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

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]

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.

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

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]

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

int *yptr ;int *yptr ;

yptr = y ;yptr = y ;

yptr ++ ;yptr ++ ;

pointer variable yPtr

y[0] y[1] y[2] y[4]y[3]

3000 3004 3008 3012 3016

location

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

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 ;

}}

yptr = y ; yptr = y ;

is same asis same as

yptr = &y [ 0 ] ;yptr = &y [ 0 ] ;……..……..

yptr = &y [ 2 ] ;yptr = &y [ 2 ] ;

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 ;

}}

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

Pointer Pointer ArithmeticArithmetic*yptr + 3 ;*yptr + 3 ;

cout << *yptr ;cout << *yptr ;

*yptr += 3 ;*yptr += 3 ;

This Is an Expression

Pointer Pointer ArithmeticArithmetic

yptr = &x ;yptr = &x ;

yptr ++ ;yptr ++ ;

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 ;

Decrementing Decrementing

*yptr --*yptr --

Pointer Pointer ArithmeticArithmetic

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

……....

p1 + p2 ;p1 + p2 ;

Error

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 ;

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 ) ;

Pointer Pointer ComparisonComparisonif ( y1 > y2 )if ( y1 > y2 )

if ( y1 >= y2 ) if ( y1 >= y2 )

if ( y1 == if ( y1 == y2 ) y2 )

Pointer Pointer ComparisonComparison

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

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 ) ;

ExampleExampleint que [ 10 ] ;int que [ 10 ] ;int y [ 10 ];int y [ 10 ];int *yptr ;int *yptr ;yptr = y ; yptr = y ; yptr = que ;yptr = que ;

pointer variable vPtr

v[0] v[1] v[2] v[4]v[3]

3000 3004 3008 3012 3016

location

StringsStrings

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’ ;

String String InitializationInitialization

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

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.

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’ ;

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’ ;

}}

In Today’s In Today’s LectureLecture

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