Array & Pointers
2 One-Dimensional Arrays  Array – A consecutive series of variables that share one name – 5 consecutive variables of integer type under the name of num int num[5] ; num 1000 1004 1008 1012 1016 Memory addresses
3 The Relationship between Arrays and Pointers  Example : – Which address does each element have? 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 Memory addresses
4 The Relationship between Arrays and Pointers  Example : What is num? – num is the constant pointer of which value is the start address of the array. int num[5] ; num 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] num == &num[0] == 1000 Memory addresses
5 The Relationship between Arrays and Pointers  Example : Arithmetic of pointers – “pointer + 1” does not mean increasing pointer by 1. – “pointer + 1” is “the address of the next element”. – “pointer – 1” is “the address of the prior element”. 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] Memory addresses
6 The Relationship between Arrays and Pointers  Example : Arithmetic of pointers 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 ; Memory addresses
7 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
8 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
9 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
10 Pointer Arithmetic and Element Size  Comparing Pointers – Relational operators (<, <=, >, >=) can be applied – Equality operators (==, !=) can be applied [Ex] p = &a[5]; q = &a[1]; p <= q; /* result is 0 */ p >= q; /* result is 1 */
11 Pointer Arithmetic and Element Size  Example: Pointer Operation 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 )?
12 Pointer Arithmetic and Element Size  Example: Pointer Operation #include <stdio.h> 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; }
Combining the * and ++ Operators  Combining the * and ++ Operators – p increased or decreased, then *p performed – p increased or decreased, then *(p-1) or *(p+1) performed – Increase or decrease 1 of a variable pointing p 13 *++p  *(++p), *--p  *(--p) *p++  *(p++), *p--  *(p--) (*p)++, (*p)--
Combining the * and ++ Operators  Combining the * and ++ Operators 14 void main() { int k, a[10], *p = a ; while( p < &a[10] ) *p++ = 0 ; } void main() { int k, a[10], *p = a ; while( p < &a[10] ) { *p = 0 ; p = p + 1 ; } }
Combining the * and ++ Operators  Combining the * and ++ Operators 15 void main() { int k, a[10], *p = &a[10] ; while( p >= &a[0] ) *--p = 0 ; } void main() { int k, a[10], *p = a ; while( p >= &a[0] ) { p = p -1 ; *p = 0 ; } }
Example  Add inputted integer numbers 16 #include <stdio.h> int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += num[k] ; return sum ; } void main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( “%d”, &a[k] ) ; printf( “%dn”, sum(a, 100) ) ; } #include <stdio.h> int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += *num++ ; return sum ; } void main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( “%d”, &a[k] ) ; printf( “%dn”, sum(a, 100) ) ; } int num[] and int *num are the same

10. array & pointer

  • 1.
  • 2.
    2 One-Dimensional Arrays  Array –A consecutive series of variables that share one name – 5 consecutive variables of integer type under the name of num int num[5] ; num 1000 1004 1008 1012 1016 Memory addresses
  • 3.
    3 The Relationship betweenArrays and Pointers  Example : – Which address does each element have? 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 Memory addresses
  • 4.
    4 The Relationship betweenArrays and Pointers  Example : What is num? – num is the constant pointer of which value is the start address of the array. int num[5] ; num 1000 1004 1008 1012 1016 num[0] num[1] num[2] num[3] num[4] num == &num[0] == 1000 Memory addresses
  • 5.
    5 The Relationship betweenArrays and Pointers  Example : Arithmetic of pointers – “pointer + 1” does not mean increasing pointer by 1. – “pointer + 1” is “the address of the next element”. – “pointer – 1” is “the address of the prior element”. 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] Memory addresses
  • 6.
    6 The Relationship betweenArrays and Pointers  Example : Arithmetic of pointers 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 ; Memory addresses
  • 7.
    7 Pointer Arithmetic andElement 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
  • 8.
    8 Pointer Arithmetic andElement 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
  • 9.
    9 Pointer Arithmetic andElement 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
  • 10.
    10 Pointer Arithmetic andElement Size  Comparing Pointers – Relational operators (<, <=, >, >=) can be applied – Equality operators (==, !=) can be applied [Ex] p = &a[5]; q = &a[1]; p <= q; /* result is 0 */ p >= q; /* result is 1 */
  • 11.
    11 Pointer Arithmetic andElement Size  Example: Pointer Operation 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 )?
  • 12.
    12 Pointer Arithmetic andElement Size  Example: Pointer Operation #include <stdio.h> 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; }
  • 13.
    Combining the *and ++ Operators  Combining the * and ++ Operators – p increased or decreased, then *p performed – p increased or decreased, then *(p-1) or *(p+1) performed – Increase or decrease 1 of a variable pointing p 13 *++p  *(++p), *--p  *(--p) *p++  *(p++), *p--  *(p--) (*p)++, (*p)--
  • 14.
    Combining the *and ++ Operators  Combining the * and ++ Operators 14 void main() { int k, a[10], *p = a ; while( p < &a[10] ) *p++ = 0 ; } void main() { int k, a[10], *p = a ; while( p < &a[10] ) { *p = 0 ; p = p + 1 ; } }
  • 15.
    Combining the *and ++ Operators  Combining the * and ++ Operators 15 void main() { int k, a[10], *p = &a[10] ; while( p >= &a[0] ) *--p = 0 ; } void main() { int k, a[10], *p = a ; while( p >= &a[0] ) { p = p -1 ; *p = 0 ; } }
  • 16.
    Example  Add inputtedinteger numbers 16 #include <stdio.h> int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += num[k] ; return sum ; } void main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( “%d”, &a[k] ) ; printf( “%dn”, sum(a, 100) ) ; } #include <stdio.h> int sum( int num[], int size ) { int k, sum = 0 ; for( k = 0 ; k < size ; k++ ) sum += *num++ ; return sum ; } void main() { int a[100], k ; for( k = 0 ; k < 100 ; k++ ) scanf( “%d”, &a[k] ) ; printf( “%dn”, sum(a, 100) ) ; } int num[] and int *num are the same