Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.

Similar presentations


Presentation on theme: "Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers."— Presentation transcript:

1 Introduction to Programming Lecture 15

2 In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers Expression Pointers Arithmetic Pointers Arithmetic Multidimensional Arrays Multidimensional Arrays Pointer String and Array Pointer String and Array

3 Pointers and Arrays 0 1 2 3 4 5 6 7 8 9 Starting Address of Array y int y [ 10 ] ; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

4 The name of the array is like a pointer which contain the address of the first element.

5 Declaration of a Pointer Variable int y [ 10 ] ; int *yptr ; yptr = y ; yptr is a pointer to integer

6 Declaration of a Pointer Variable 0 1 2 3 4 5 6 7 8 9 y [ 3 ] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

7 int y [ 10 ] ; int *yptr ; yptr = y ; yptr ++ ;

8 pointer variable yPtr y[0]y[1]y[2]y[4]y[3] 30003004300830123016 location

9 In this case yptr is a pointer to integer so now when we increment yptr it points to the next integer

10 Example 1 #include<iostream.h> main ( ) { int y [ 10 ] ; int *yptr = y ; yptr = y ; cout << yptr ; yptr ++ ; cout << yptr ; }

11 yptr = y ; is same as yptr = &y [ 0 ] ; …….. yptr = &y [ 2 ] ;

12 Example 2 #include<iostream.h> main ( ) { int y [ 10 ] ; int *yptr ; yptr = y ; cout << yptr ; yptr ++ ; cout << *yptr ; }

13 main ( ) { int x = 10 ; int *yptr ; yptr = &x ; cout << yptr ; cout << *yptr ; *yptr ++ ; } Example 3 increment whatever yptr points to

14 Pointer Arithmetic *yptr + 3 ; cout << *yptr ; *yptr += 3 ; This Is an Expression

15 Pointer Arithmetic yptr = &x ; yptr ++ ;

16 Pointer Arithmetic int x =10 ; int *yptr ; yptr = &x ; *yptr += 3 ; yptr += 3 ;

17 Decrementing *yptr --

18 Pointer Arithmetic int *p1,*p2; ….. p1 + p2 ; Error

19 Pointer Arithmetic int y [ 10 ], *y1, *y2 ; y1 = &y [ 0 ] ; y1 = &y [ 0 ] ; y2 = &y [ 3 ] ; y2 = &y [ 3 ] ; cout << y2 - y1 ; cout << y2 - y1 ;

20 Pointer Arithmetic int y [ 10 ] ; int *yptr ; yptr = y [ 5 ] ; cout << *( yptr + 5 ) ;

21 Pointer Comparison if ( y1 > y2 ) if ( y1 >= y2 ) if ( y1 == y2 )

22 Pointer Comparison if ( *y1 > *y2 )

23 Example int y [ 10 ] ; int *yptr ; yptr = y ; cout << y [ 5 ] ; cout << ( yptr + 5 ) ; cout << *( yptr + 5 ) ;

24 Example int que [ 10 ] ; int y [ 10 ]; int *yptr ; yptr = y ; yptr = que ;

25 pointer variable vPtr v[0]v[1]v[2]v[4]v[3] 30003004300830123016 location

26 Strings

27 String Initialization char name [ 20 ] ; name [ 0 ] = ‘A’ ; name [ 1 ] = ‘m’ ; name [ 2 ] = ‘i’ ; name [ 3 ] = ‘r’ ; name [ 4 ] = ‘\0’ ;

28 String Initialization Strings is always terminated with \0

29 String Initialization char name [ 20 ] = “Amir” ; Array must be one character space larger than the number of printable character which are to be stored. Array must be one character space larger than the number of printable character which are to be stored.

30 Example 4 char string1 [ 20 ] = “Amir”; char string2 [ 20 ] ; char *ptrA, *ptrB ; prtA = string1 ; prtB = string2 ; while ( *ptrA != ‘\0’ ) { *ptrB++ = *ptrA++; } *ptrB = ‘\0’ ;

31 String Copy Function myStringCopy ( char *destination, const char *source ) { while ( *source != ‘\0’ ) { *destination++ = *source++ ; *destination++ = *source++ ;} *destination = ‘\0’ ; }

32 In Today’s Lecture Pointers and Arrays Pointers and Arrays Pointer Arithmetic Pointer Arithmetic Manipulation of Arrays Manipulation of Arrays String Arrays String Arrays


Download ppt "Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers."

Similar presentations


Ads by Google