Lecture 18: The Elegant, Abstract World of Computing

27
Lecture 18: The Elegant, Abstract World of Computing

description

Lecture 18: The Elegant, Abstract World of Computing. Arrays and Pointers. Arrays and pointers closely related Array name like a constant pointer Arrays passed to a function by reference Pointers can do array subscripting operations. Relationship between Pointers and Arrays. - PowerPoint PPT Presentation

Transcript of Lecture 18: The Elegant, Abstract World of Computing

Page 1: Lecture 18:  The Elegant, Abstract World of Computing

Lecture 18: The Elegant, Abstract World of Computing

Page 2: Lecture 18:  The Elegant, Abstract World of Computing

Arrays and PointersArrays and pointers closely related

Array name like a constant pointerArrays passed to a function by referencePointers can do array subscripting operations

Page 3: Lecture 18:  The Elegant, Abstract World of Computing

Relationship between Pointers and Arrays

Define an array b[5] and a pointer bPtr int b[5]; int *bPtr;

To set them equal to one another usebPtr = b;

The array name (b) is actually the address of first element of the array.

bPtr = &b[ 0 ];Explicitly assigns the address of first element of b to bPtr.

Element b[ 3 ]Can be accessed by *(bPtr + 3) --- pointer/offset notation

where 3 is called the offset.Can be accessed by bPtr[ 3 ] --- pointer/subscript notationbPtr[ 3 ] same as b[ 3 ]Can also be accessed by *(b+3) --- view b as a constant pointer

*b accesses to b[0]

Page 4: Lecture 18:  The Elegant, Abstract World of Computing

Relationship between Pointers and ArraysWhat is the difference?

int b[5], a[5]; int *objPtr;

A pointer can be assigned to point to different objects.objPtr = b;objPtr = a;

Array name likes a constant pointer.objPtr = b;a = objPtr;b = a;

PointersDereferencing a NULL pointer is NOT allowed.

int *aPtr = NULL, *bPtr = 0;int x;x = *aPtr;x = * bPtr;

Dereferencing a pointer not pointing to an object is NOT allowed.int *aPtr , x;x = *aPtr;

Page 5: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; }return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), just before call into sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

Page 6: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; }return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), while calling into sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

0

Page 7: Lecture 18:  The Elegant, Abstract World of Computing

0

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), just after execute “*sum = 0;”

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

Page 8: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), …

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

0

Page 9: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), after execute “*sum += x[k];”

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

0

1

Page 10: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), increment k

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

0

1

1

Page 11: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), perform addition

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

1

1

1

3

Page 12: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), increment k

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

1

3

2

Page 13: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), perform addition

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

0

3

2

6

Page 14: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), right after the for loop

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0xBFFFF840

k0xBFFFF85C

05

15

Page 15: Lecture 18:  The Elegant, Abstract World of Computing

Passing Arrays by Reference

Array name like a constant pointer

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int *sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int *sum ) { int k;

*sum = 0; for (k = 0; k < size; k++) { *sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), right after call sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

15

Page 16: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-ReferencePassing array by referencePassing integer/char/float by value#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k]; }return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), just before call into sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

Page 17: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k]; }return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), while calling into sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0

k0xBFFFF85C

0

Passing array by referencePassing integer/char/float by value

Page 18: Lecture 18:  The Elegant, Abstract World of Computing

0

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), just after execute “sum = 0;”

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

0

k0xBFFFF85C

0

Passing array by referencePassing integer/char/float by value

Page 19: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), …

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

k0xBFFFF85C

0

0

0

Passing array by referencePassing integer/char/float by value

Page 20: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), after execute “sum += x[k];”

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

k0xBFFFF85C

0

01

Passing array by referencePassing integer/char/float by value

Page 21: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), increment k

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

k0xBFFFF85C

01

1

Passing array by referencePassing integer/char/float by value

Page 22: Lecture 18:  The Elegant, Abstract World of Computing

1

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), perform addition

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

k0xBFFFF85C

01

3

Passing array by referencePassing integer/char/float by value

Page 23: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In sumRef(), right after the for loop

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

x0xBFFFF850

0xBFFFF82C

size0xBFFFF854

5

sum0xBFFFF858

k0xBFFFF85C

05

15

Passing array by referencePassing integer/char/float by value

Page 24: Lecture 18:  The Elegant, Abstract World of Computing

Call-by-Value via Call-by-Reference

#include<stdio.h>

#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k];}return;}

……

…a[0]

0xBFFFF82C1

RAM

In main(), right after call sumRef()

2

3

4

5

a[1]0xBFFFF830

a[2]0xBFFFF834

a[3]0xBFFFF838

a[4]0xBFFFF83C

total0xBFFFF840

0

Passing array by referencePassing integer/char/float by value

Page 25: Lecture 18:  The Elegant, Abstract World of Computing

In-class Programming Assignment

#include <stdio.h>#define SIZE 3000#define WINDOW 10

void readin(double sonar[SIZE]);void denoising(double *sonarPtr, double *rPtr);

int main (){double sonar[SIZE];double denoisedSonar[SIZE];

readin(sonar);denoising(sonar, denoisedSonar);return 0;

}

void denoising(double *sonarPtr, double *rPtr){ /* You need to finish this function */ For every possible starting index j of the sliding window, sum = 0; compute the total sum of the sonar data inside the sliding window; rPtr[j] = sum / WINDOW;

}

void readin(double sonar[SIZE]){}

7.034 0.361 2.550 11.588 19.166 18.317 9.821 1.490 0.983 8.766 17.684 19.537 12.622 3.296 0.134 6.043 15.590 19.998 15.214 5.636 0.071 3.634 13.050

j

Page 26: Lecture 18:  The Elegant, Abstract World of Computing

Practice Question

Q. What is the output of the following program?

#include<stdio.h>#define SIZE 5void sumRef( int *x, int size, int sum );

int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d %d\n”, *a, total); return 0; }

void sumRef( int *x, int size, int sum ) { int k;

sum = 0; for (k = 0; k < size; k++) { sum += x[k]; }for (k = 0; k < size; k++) { x[k] *= 3; }}

A. 3 15B. 1 15C. 3 0D. 1 0

Solu

tion

: C

Page 27: Lecture 18:  The Elegant, Abstract World of Computing

Practice Question

Q. To assign the third element b[2] of an array b to the variable x, which of the following is WRONG?

int x, b[5] = {1, 2, 3, 4, 5};int *bPtr;

A. bPtr = b;x = *(bPtr+2);

B. bPtr = b;x = bPtr[2];

C. x = *(b+2);

D. b = b + 2;x = *b;

E. bPtr = b;bPtr += 2;x = *bPtr; S

olu

tion

: D