c programming language: arrays and dynamic...

90
C Programming Language: Arrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thu Jan 23, 2008 Lecture 4

Upload: others

Post on 25-Apr-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

C Programming Language:

Arrays and Dynamic Allocation

Math 230

Assembly Language Programming

(Computer Organization)

Thu Jan 23, 2008

Lecture 4

Page 2: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

2

Learning Objectives

• Pointer Review, and call-by-reference

• Array Names as Pointers

� Indirect referencing: Convert integer indexed array notation to pointer notation

• Pointer Arithmetic

� Use pointers vs indices for array processing

• Passing and Using Array Addresses

� pointer declaration vs standard for formal parameter

• Dynamic Memory

� 1d, 2d

Page 3: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

3

Easy Steps to Pointers

• Step 1: Declare the variable to be pointed to

int num;

char ch = ‘A’;

float x;

num:

‘A’ch:

x:

Page 4: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

4

Easy Steps to Pointers (cont)

• Step 2: Declare the pointer variable

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

NULL

NULL

NULL

Page 5: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

Page 6: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

Page 7: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

Page 8: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

chPtr = &ch;

Page 9: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

chPtr = &ch;

addr of ch

Page 10: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

chPtr = &ch;

addr of ch

xPtr = &x;

Page 11: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

chPtr = &ch;

addr of ch

xPtr = &x;

addr of x

Page 12: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

5

Easy Steps to Pointers (cont)

• Step 3: Assign address of variable to pointer

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

numPtr = #

addr of num

chPtr = &ch;

addr of ch

xPtr = &x;

addr of x

A pointer’s type has to correspond to the type of the variable it points to

Page 13: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

6

Easy Steps to Pointers (cont)

• Step 4: De-reference the pointers

int* numPtr = NULL;

char *chPtr = NULL;

float * xPtr = NULL;

int num;

char ch = ‘A’;

float x;

numPtr = #

chPtr = &ch;

xPtr = &x;

*xPtr = 0.25;

*numPtr = *chPtr;

num: 65

‘A’ch:

0.25x:

numPtr: addr of num

addr of chchPtr:

addr of xxPtr:

Page 14: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

7

Your Turn…

• Write a fragment of C code that does the

following:

� Declares 3 integer variables called a, b, and c.

� Declares 3 integer pointers p1, p2, and p3.

� Assigns the values 34, 10, and –4 to a, b, and c

� Initializes p1 with the addres of a and initializes p2

with the address of b

� Points p3 to the same item pointed to by p2

� Prints out the contents pointed to by p1, p2 and p3

C Source File

Page 15: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

8

Pointers and Function Parameters

• Example: Function to swap the values of

two variables

x: 1

y: 2

swapx: 2

y: 1

Page 16: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

9

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

Bad swap

Page 17: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

10

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

Bad swap

Page 18: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

11

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

1

2

a:

b:

tmp:

Bad swap

Page 19: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

12

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

1

2

a:

b:

1tmp:

Bad swap

Page 20: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

13

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

2

2

a:

b:

1tmp:

Bad swap

Page 21: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

14

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

2

1

a:

b:

1tmp:

Bad swap

Page 22: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

15

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

2

1

a:

b:

1tmp:

Bad swap

Page 23: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

15

#include <stdio.h>

void swap1(int a, int b){

int tmp;

tmp = a;a = b;b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap1(x, y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

2

1

a:

b:

1tmp:

Bad swap

Page 24: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

16

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

Good swap

Page 25: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

17

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

Good swap

Page 26: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

18

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

addr of x

addr of y

a:

b:

tmp:

Good swap

Page 27: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

19

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

1

2

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 28: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

20

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

2

2

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 29: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

21

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

2

1

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 30: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

22

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

2

1

x:

y:

Good swap

Page 31: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

22

#include <stdio.h>

void swap2(int* a, int* b){

int tmp;

tmp = *a;*a = *b;*b = tmp;return;

}

int main(){

int x = 1, y = 2;

swap2(&x, &y);printf(“%d %d\n”, x, y);return 0;

}

2

1

x:

y:

Good swap

Page 32: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

Page 33: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

• Pointers are closely associated with array names

Page 34: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

• Pointers are closely associated with array names

• Subscripts are related to the true address of an array

element

� The array element’s address is determined from the address of

the first element, and the size of the element

Page 35: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

• Pointers are closely associated with array names

• Subscripts are related to the true address of an array

element

� The array element’s address is determined from the address of

the first element, and the size of the element

• Given grades[4], and that an integer is stored as four

bytes, the computer calculates the address as:

Page 36: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

• Pointers are closely associated with array names

• Subscripts are related to the true address of an array

element

� The array element’s address is determined from the address of

the first element, and the size of the element

• Given grades[4], and that an integer is stored as four

bytes, the computer calculates the address as:

&grades[4] = &grades[0] + (4 * (4 bytes) )

Page 37: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

23

Array Name as A Pointer

• Pointers are closely associated with array names

• Subscripts are related to the true address of an array

element

� The array element’s address is determined from the address of

the first element, and the size of the element

• Given grades[4], and that an integer is stored as four

bytes, the computer calculates the address as:

&grades[4] = &grades[0] + (4 * (4 bytes) )

address of first element

plus a 16 byte offset

(1 int = 4 bytes)

Page 38: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

24

Pointer Arithmetic

grades[0]

grades[1]

grades[2]

grades[3]

0xA201

0xA205

0xA209

0xA20C

Page 39: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

24

Pointer Arithmetic

grades[0]

grades[1]

grades[2]

grades[3]

0xA201

0xA205

0xA209

0xA20C

• Used to calculate the address of any array element

Page 40: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

24

Pointer Arithmetic

grades[0]

grades[1]

grades[2]

grades[3]

0xA201

0xA205

0xA209

0xA20C

• Used to calculate the address of any array element

• If each integer takes up 4 bytes of memory, the address of grades[2] can be calculated from the address of

grades[0] plus 8

Page 41: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

24

Pointer Arithmetic

grades[0]

grades[1]

grades[2]

grades[3]

0xA201

0xA205

0xA209

0xA20C

• Used to calculate the address of any array element

• If each integer takes up 4 bytes of memory, the address of grades[2] can be calculated from the address of

grades[0] plus 8

• The address of grades[3] is the address of grades[0]

plus 12 grades[0]

grades[1]

0xA201

0xA205

Page 42: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

25

Using Pointers to Access Array Elements

• We can create a pointer to store the address of the first array element

� an array of integers requires an int*

� an array of characters requires a char*

• This allows us to access any individual element in the array using a pointer

Page 43: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

25

Using Pointers to Access Array Elements

• We can create a pointer to store the address of the first array element

� an array of integers requires an int*

� an array of characters requires a char*

• This allows us to access any individual element in the array using a pointer

int myArray[4];

int* arrPtr;

arrPtr = &myArray[0];

Page 44: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

25

Using Pointers to Access Array Elements

• We can create a pointer to store the address of the first array element

� an array of integers requires an int*

� an array of characters requires a char*

• This allows us to access any individual element in the array using a pointer

int myArray[4];

int* arrPtr;

arrPtr = &myArray[0];

char myNames[20];

char* charPtr;

charPtr = &myNames[0];

Page 45: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

26

Walk an array: conventional

#include <stdio.h>

int main()

{

int i;

int grades[] = {98, 87, 92, 79, 85};

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, grades[i] );

}

return 0;

}

Without using pointers

Page 46: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

Page 47: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

declare a pointer to an int

Page 48: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

declare a pointer to an int

store the starting array address

Page 49: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

declare a pointer to an int

store the starting array address

(gPtr + 1) = &grades[1]

Page 50: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

declare a pointer to an int

store the starting array address

(gPtr + 1) = &grades[1]

(gPtr + 2) = &grades[2]

Page 51: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

27

Walk an array: pointer based

#include <stdio.h>

int main()

{

int *gPtr;

int i;

int grades[] = {98, 87, 92, 79, 85};

gPtr = &grades[0];

for (i = 0; i <= 4; ++i){

printf("\nElement %d is %d", i, *(gPtr + i) );

}

return 0;

}

Using pointers for array access

declare a pointer to an int

store the starting array address

(gPtr + 1) = &grades[1]

(gPtr + 2) = &grades[2]

(gPtr + 3) = &grades[3]

Page 52: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

Page 53: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

• The second example, shows how the computer internally accesses array elements

Page 54: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

• The second example, shows how the computer internally accesses array elements

• Subscripts are automatically converted to their equivalent pointer

Page 55: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

• The second example, shows how the computer internally accesses array elements

• Subscripts are automatically converted to their equivalent pointer

• The expression (gPtr + i) calculates the

address

� *(gPtr + i) is used to “dereference”

Page 56: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

• The second example, shows how the computer internally accesses array elements

• Subscripts are automatically converted to their equivalent pointer

• The expression (gPtr + i) calculates the

address

� *(gPtr + i) is used to “dereference”

• Note that we are using an integer value in the addition

Page 57: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

28

Discussion on Examples – More Pointer Math

• The second example, shows how the computer internally accesses array elements

• Subscripts are automatically converted to their equivalent pointer

• The expression (gPtr + i) calculates the

address

� *(gPtr + i) is used to “dereference”

• Note that we are using an integer value in the addition

• The offset added to gPtr is automatically scaled

� +1 = 1 integer “jump” or

� +1 = 1 float “jump” etc.

Page 58: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

29

More Pointer Math and Array Equivalency

Page 59: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

29

More Pointer Math and Array Equivalency

• Are the parentheses necessary in the

expression *(gPtr + 3)?

Page 60: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

29

More Pointer Math and Array Equivalency

• Are the parentheses necessary in the

expression *(gPtr + 3)?

• Yes. Note the difference between:

*(gPtr + 3) and *gPtr+3

� BIG difference. The parentheses are required

Page 61: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

29

More Pointer Math and Array Equivalency

• Are the parentheses necessary in the

expression *(gPtr + 3)?

• Yes. Note the difference between:

*(gPtr + 3) and *gPtr+3

� BIG difference. The parentheses are required

• Finally, the expression grades[i] can

always be replaced with *(grades + i)

Page 62: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Page 63: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

Page 64: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

int a[10];

Page 65: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

int a[10];

int *pa;

Page 66: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

int a[10];

int *pa;

Important difference between an array name and a pointer variable:

Page 67: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

int a[10];

int *pa;

Important difference between an array name and a pointer variable:

� A pointer is a variable so it’s legal to use

• pa = a;

• pa++;

Page 68: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

30

Pointer Constants: Array Names

Assume the following declarations have been made:

int a[10];

int *pa;

Important difference between an array name and a pointer variable:

� A pointer is a variable so it’s legal to use

• pa = a;

• pa++;

� An array name is a constant, not a variable. ILLEGAL usage

• a = pa; //Incompatible types int* and int[5]

• a++; //Not allowed

• pa = &a[0]; //Both types must be same

Page 69: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

31

Pointer Constants

Page 70: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

31

Pointer Constants

• When arrays are created, an internal “pointer constant” is automatically created

Page 71: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

31

Pointer Constants

• When arrays are created, an internal “pointer constant” is automatically created

• This pointer constant stores the starting address of the array, i.e., the first element

Page 72: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

31

Pointer Constants

• When arrays are created, an internal “pointer constant” is automatically created

• This pointer constant stores the starting address of the array, i.e., the first element

• What happens when an array is declared?

1. The array name becomes the name of a pointer constant

2. The first array element is stored at the pointer’s address

3. Storage is created for the appropriate number of the indicated variable type

Page 73: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

32

#include <stdio.h>

int main()

{

int i;

int grades[] = {98, 87, 92, 79, 85};

for (i = 0; i <= 4; ++i)

printf("\nElement %d is %d", i, *(grades + i) );

return 0;

}

Init’z as array, work as pointer

Page 74: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

32

#include <stdio.h>

int main()

{

int i;

int grades[] = {98, 87, 92, 79, 85};

for (i = 0; i <= 4; ++i)

printf("\nElement %d is %d", i, *(grades + i) );

return 0;

}

Init’z as array, work as pointerA ‘constant’ pointer

named grades is

automatically

generated

Init’z as array, work as pointer

Page 75: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

32

#include <stdio.h>

int main()

{

int i;

int grades[] = {98, 87, 92, 79, 85};

for (i = 0; i <= 4; ++i)

printf("\nElement %d is %d", i, *(grades + i) );

return 0;

}

Init’z as array, work as pointerA ‘constant’ pointer

named grades is

automatically

generated

Init’z as array, work as pointer

Pointer notation

used although no

explicit pointer

declared!

Page 76: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

Page 77: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

address of wilma[0] assigned to p

Page 78: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

address of wilma[0] assigned to p

wilma[0] is assigned to x

Page 79: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

address of wilma[0] assigned to p

wilma[0] is assigned to x

wilma[1] is assigned to x

Page 80: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

address of wilma[0] assigned to p

wilma[0] is assigned to x

wilma[1] is assigned to x

&wilma[1] is assigned to p

Page 81: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

34

Pointer Arithmetic – Marching Through Arrays

• You can use pointers to index through arrays by

pointing to each element in turn

• Given that p points to wilma[i], p + k points to

wilma[i + k]int wilma[4], i,*p, x;

wilma[4]={21, 9, 19, 6};

p = &wilma[0];

x = *p;

x = *(p+1);

p = p + 1;

p++;

address of wilma[0] assigned to p

wilma[0] is assigned to x

wilma[1] is assigned to x

&wilma[1] is assigned to p

p now points to wilma[2]

Page 82: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

35

Marching Through Arrays – an example

• *p++ can be used to walk through the array pointed to by p

• Given int wilma[4], i,*p, x;

� The same output is achieved by both of the following

Page 83: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

35

Marching Through Arrays – an example

• *p++ can be used to walk through the array pointed to by p

• Given int wilma[4], i,*p, x;

� The same output is achieved by both of the following

p = wilma;

for (i = 0; i < 4; i++)

printf(“%d\n”, *p++);

Page 84: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

35

Marching Through Arrays – an example

• *p++ can be used to walk through the array pointed to by p

• Given int wilma[4], i,*p, x;

� The same output is achieved by both of the following

p = wilma;

for (i = 0; i < 4; i++)

printf(“%d\n”, *p++);

for (i = 0; i < 4; i++)

printf(“%d\n”, wilma[i]);

Page 85: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

37

Array Marching – Stop Based on Address

#include <stdio.h>

int main()

{

int nums[5] = {16, 54, 7, 43, -5};

int total = 0, *nPtr;

nPtr = nums;

while (nPtr <= nums + 4)

total += *nPtr++;

printf("The total of the array elements is %d", total);

return 0;

}

Page 86: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

37

Array Marching – Stop Based on Address

#include <stdio.h>

int main()

{

int nums[5] = {16, 54, 7, 43, -5};

int total = 0, *nPtr;

nPtr = nums;

while (nPtr <= nums + 4)

total += *nPtr++;

printf("The total of the array elements is %d", total);

return 0;

}

store address of nums[0] in nPtr

Page 87: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

37

Array Marching – Stop Based on Address

#include <stdio.h>

int main()

{

int nums[5] = {16, 54, 7, 43, -5};

int total = 0, *nPtr;

nPtr = nums;

while (nPtr <= nums + 4)

total += *nPtr++;

printf("The total of the array elements is %d", total);

return 0;

}

store address of nums[0] in nPtr

compare addresses

Page 88: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

38

Functions, Pointers and Using Array Addresses

• The array address is the only actual item

passed

� What constitutes an array’s address?

• The following examples study the passing of

arrays and pointers

Page 89: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

39

findMax routine

int findMax(int vals[], int numEls)

{

int i, max = vals[0];

for (i = 1; i < numEls; ++i)

if (max < vals[i])

max = vals[i];

return(max);

}

Page 90: C Programming Language: Arrays and Dynamic …dept.swccd.edu/bsmith/m230/lectures/04CpointersAndArrays.pdfArrays and Dynamic Allocation Math 230 Assembly Language Programming (Computer

40

pgm 8.6c findMax routine-changes

int findMax(int *vals, int numEls)

{

int i, max = *vals;

for (i = 1; i < numEls; ++i)

if (max < *(vals + i) )

max = *(vals + i);

return(max);

}

int vals[]

max = &vals[0]

max = vals[i]