10. array & pointer
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

10. array & pointer

on

  • 465 views

 

Statistics

Views

Total Views
465
Views on SlideShare
256
Embed Views
209

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 209

http://class.skku.goorm.io 209

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

10. array & pointer Presentation Transcript

  • 1. Array & Pointers
  • 2. 2One-Dimensional Arrays Array– A consecutive series of variables that share one name– 5 consecutive variables of integer type under the name of numint num[5] ;num1000 1004 1008 1012 1016 Memory addresses
  • 3. 3The Relationship between Arrays andPointers Example :– Which address does each element have?int num[5] ;num1000 1004 1008 1012 1016num[0] num[1] num[2] num[3] num[4]&num[0] == 1000&num[1] == 1004&num[2] == 1008&num[3] == 1012&num[4] == 1016Memory addresses
  • 4. 4The Relationship between Arrays andPointers Example : What is num?– num is the constant pointer of which value is the start address ofthe array.int num[5] ; num1000 1004 1008 1012 1016num[0] num[1] num[2] num[3] num[4]num == &num[0] == 1000Memory addresses
  • 5. 5The Relationship between Arrays andPointers 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 1016num[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. 6The Relationship between Arrays andPointers Example : Arithmetic of pointersint num[5] ; 10 20 30 40 501000 1004 1008 1012 1016num[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. 7Pointer 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 9pa0 1 2 3 4 5 6 7 8 9pa0 1 2 3 4 5 6 7 8 9paqq
  • 8. 8Pointer 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 9pa0 1 2 3 4 5 6 7 8 9pa0 1 2 3 4 5 6 7 8 9paqq
  • 9. 9Pointer 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 9qap
  • 10. 10Pointer 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. 11Pointer Arithmetic and Element Size Example: Pointer Operationint 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. 12Pointer 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;}
  • 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 p13*++p  *(++p), *--p  *(--p)*p++  *(p++), *p--  *(p--)(*p)++, (*p)--
  • 14. Combining the * and ++ Operators Combining the * and ++ Operators14void 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 ++ Operators15void 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 inputted integer numbers16#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 *numare the same