Upload
shyanne-leavey
View
255
Download
11
Embed Size (px)
Citation preview
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]
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
int x =10 ;int x =10 ;
int *yptr ;int *yptr ;
yptr = &x ;yptr = &x ;
*yptr += 3 ; *yptr += 3 ;
yptr += 3 ;yptr += 3 ;
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 )
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 ;
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’ ;
}}