11. array & pointer

Download 11. array & pointer

Post on 19-May-2015

121 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • 1. Array & Pointers

2. 2 One-Dimensional Arrays Array type data type. num 10 int 5 int num[5] ; num 1000 1004 1008 1012 1016 3. 3 One-Dimensional Arrays Array index . index = 0 . int num[5] ; num[0] = 10 ; num[1] = 13 ; num[2] = 14 ; num[3] = 17 ; num[4] = 20 ; 10 13 14 17 20num 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] 4. 4 One-Dimensional Arrays Array Syntax . 0 . grade[0], grade[1],~ , grade[49] . element-type array_name[size]; [Ex] int grade[50]; data type variable Name size of Array 5. 5 One-Dimensional Arrays #include int main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( %d, &a[k] ) ; for( k = 99 ; k >= 0 ; k-- ) printf( %d , a[k] ) ; printf( n ) ; return 0; } #include int main() { int a[100], k, sum = 0 ; for( k = 0 ; k < 100 ; k++ ) scanf( %d, &a[k] ) ; for( k = 0 ; k < 100 ; k++ ) sum += a[k] ; printf( %dn, sum ) ; return 0; } 6. 6 Initialization array . float x[7] = { -1.1, 0.2, 33.0, 4.4, 5.05, 0.0, 7.7 }; x[0] = -1.1, x[1] = 0.2,, x[6] = 7.7 . int a[100] = { -1 }; a[0] = -1, a[1] = 0, a[99] = 0 0 . 7. 7 cnt_abc Program #include #include int main(void) { int letter[26] = {0}, c, i ; while ( (c = getchar( )) != EOF) { c=toupper(c); if( isalph(c) ) ++letter[c A]; } for ( i = 0; i < 26; ++i) { if ( i % 6 == 0 ) printf(n); printf(%4c:%3d, A + i, letter[i]); } /* end of for */ return 0; } getchar() . . . 8. 8 The Relationship between Arrays and Pointers Example : ? int num[5] ; num 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] &num[0] == 1000 &num[1] == 1004 &num[2] == 1008 &num[3] == 1012 &num[4] == 1016 9. 9 The Relationship between Arrays and Pointers Example : num ? num . int num[5] ; num 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] num == &num[0] == 1000 10. 10 The Relationship between Arrays and Pointers Example : + 1 1 , - 1 int num[5] ; 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] num == &num[0] == 1000 (num+0) == ?? (num+1) == ?? (num+2) == ?? (num+3) == ?? (num+4) == ?? &num[0] &num[1] &num[2] &num[3] &num[4] 11. 11 The Relationship between Arrays and Pointers Example : int num[5] ; 10 20 30 40 50 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] int num[5], *p = num ; *p = 10 ; *(p+1) = 20 ; *(p+2) = 30 ; *(p+3) = 40 ; *(p+4) = 50 ; int num[5], *p = num ; p[0] = 10 ; p[1] = 20 ; p[2] = 30 ; p[3] = 40 ; p[4] = 50 ; int num[5] ; *num = 10 ; *(num+1) = 20 ; *(num+2) = 30 ; *(num+3) = 40 ; *(num+4) = 50 ; int num[5] ; num[0] =10 ; num[1] = 20 ; num[2] = 30 ; num[3] = 40 ; num[4] = 50 ; 12. 12 Pointer Arithmetic and Element Size p p + 1 type access . . . . 13. 13 Pointer Arithmetic and Element Size Adding an Integer to a Pointer [Ex] p = &a[2]; q = p + 3; p += 6; 0 1 2 3 4 5 6 7 8 9 p a 0 1 2 3 4 5 6 7 8 9 p a 0 1 2 3 4 5 6 7 8 9 p a q q 14. 14 Pointer Arithmetic and Element Size Subtracting an Integer from a Pointer [Ex] p = &a[8]; q = p - 3; p -= 6; 0 1 2 3 4 5 6 7 8 9 p a 0 1 2 3 4 5 6 7 8 9 p a 0 1 2 3 4 5 6 7 8 9 p a q q 15. 15 Pointer Arithmetic and Element Size Subtracting Pointers [Ex] p = &a[5]; q = &a[1]; i = p q; /* i == 4 */ i = q p; /* i == -4 */ 0 1 2 3 4 5 6 7 8 9 q a p 16. 16 Pointer Arithmetic and Element Size Comparing Pointers (relational operators) = . (equality operators) ==, != . [Ex] p = &a[5]; q = &a[1]; p = q; /* result is 1 */ 17. 17 Pointer Arithmetic and Element Size int a[ ] = { 5,15,25,43,12,1,7,89,32,11} int *p = &a[1], *q = &a[5] ; 1. *(p + 3) ? 2. *(q - 2) ? 3. q - p ? 4. if ( p > q ) ? 5. if ( *p > *q )? 18. 18 Pointer Arithmetic and Element Size #include int main(void) { double a[2], *p, *q; p = &a[0]; /* points at base of array */ q = p + 1; /* equivalent to q = &a[1]; */ printf(%dn, q p ); printf(%dn, (int) q (int) p ); printf(%dn, sizeof(double) ); return 0; } 19. Combining the * and ++ Operators Combining the * and ++ Operators p 1 *p *p p 1 p 1 19 *++p *(++p), *--p *(--p) *p++ *(p++), *p-- *(p--) (*p)++, (*p)-- 20. Combining the * and ++ Operators Combining the * and ++ Operators 20 int main() { int k, a[10], *p = a ; while( p < &a[10] ) *p++ = 0 ; return 0; } int main() { int k, a[10], *p = a ; while( p < &a[10] ) { *p = 0 ; p = p + 1 ; } return 0; } 21. Combining the * and ++ Operators Combining the * and ++ Operators 21 int main() { int k, a[10], *p = &a[10] ; while( p >= &a[0] ) *--p = 0 ; return 0; } int main() { int k, a[10], *p = a ; while( p >= &a[0] ) { p = p -1 ; *p = 0 ; } return 0; } 22. Example 22 #include int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += num[k] ; return sum ; } int main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( %d, &a[k] ) ; printf( %dn, sum(a, 100) ) ; return 0; } #include int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += *num++ ; return sum ; } int main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( %d, &a[k] ) ; printf( %dn, sum(a, 100) ) ; return 0; } int num[] int *num .

Recommended

View more >