+ All Categories
Home > Documents > Arrays and Pointers - Montana State University and Pointers CSCI 112: Programming in C 2 Quick...

Arrays and Pointers - Montana State University and Pointers CSCI 112: Programming in C 2 Quick...

Date post: 19-Apr-2018
Category:
Upload: nguyenxuyen
View: 217 times
Download: 4 times
Share this document with a friend
29
1 Arrays and Pointers CSCI 112: Programming in C
Transcript

1

Arrays and Pointers

CSCI 112: Programming in C

2

Quick review

2

• A pointer is a variable that …

3

Quick review

3

• A pointer is a variable that takes the address of another variable as a value

4

Quick review

4

• A pointer is a variable that takes the address of another variable as a value

• The name of an array is …

5

Quick review

5

• A pointer is a variable that takes the address of another variable as a value

• The name of an array is a pointer to the first element in the array.

6

An example

6

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

7

An example

7

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

8

An example

8

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

• The base address of this array is 1000

9

An example

9

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

• The base address of this array is 1000

• Where does scores point to?

10

An example

10

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

• The base address of this array is 1000

• Where does scores point to?

scores

11

An example

11

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

• The base address of this array is 1000

• Where does scores point to?

• What is the value of scores?

scores

12

An example

12

int scores[6] = {85, 79, 100, 95, 68, 89};

In C…

In memory…

• The base address of this array is 1000

• Where does scores point to?

• What is the value of scores?

• (It’s 1000, the address of the first element)

scores

13

Another example

13

We’ve declared an int pointer and an int array.

scores points to the first element.

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

scores ptr

14

Another example

14

We set ptr to the address of the first element of scores

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

scores ptr

ptr = &scores[0];

15

Another example

15

We set ptr to the address of the first element of scores

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

scores ptr

ptr = &scores[0];

Evaluate this statement:ptr == scores

16

Another example

16

What is the value of t?int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

1 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

17

Another example

17

What is the value of t?

We follow ptr back to the first element in arr– so 1!

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

1 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

18

Another example

18

Which variables does this affect?

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

1 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

*ptr = 0;

19

Another example

19

Which variables does this affect?

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

0 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

*ptr = 0;

20

Another example

20

Which variables does this affect?

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

0 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

*ptr = 0;

ptr = scores;

21

Another example

21

Which variables does this affect?

None!Itsetsptr toaddressoffirstelementinscores…nochange

int scores[6] = {1, 2, 3, 4, 5, 6};int *ptr;

0 2 3 4 5 6

scores ptr

ptr = &scores[0];

int t = *ptr;

*ptr = 0;

ptr = table;

// Equivalent to writing ptr = &scores[0];

22

Pointer arithmetic: some powerful stuff

22

• Pointer arithmetic is an alternative to array indexing.

• If ptr is a pointer to a particular element in an array, then ptr+1 points to the next element.

1 2 3 4 5 6

ptr ptr+1

23

Pointer arithmetic: some powerful stuff

23

• Why does this work?

• When dealing with pointers, any arithmetic you do is in context of the size of that variable.

• For example, if we have an char pointer (a char is 1 byte) then writing ptr + 1 moves us 1 byte forward in memory.

• If we have an int pointer (where an int is often 4 bytes) then writing ptr + 1 moves us 4 bytes forward in memory.

• So, ptr + 1 always moves one “cell” forward.

24

Pointer arithmetic: simple exampleUsing subscripts, we’d set all elements in an array my_arr to 0 as follows:

24

for (i=0; i < SIZE; i++)my_arr[i] = 0;

25

Pointer arithmetic: simple exampleUsing subscripts, we’d set all elements in an array my_arr to 0 as follows:

The same can be done using pointer arithmetic:

25

for (i=0; i < SIZE; i++)my_arr[i] = 0;

for (i=0; i < SIZE; i++)*(my_arr+i) = 0;

26

Let’s break that last line apart…

table+i moves from the first element of the array (which table points to) to the 1+ith. For example, when i =1, table+i points to the second element.

26

*(table+i) = 0;

27

Let’s break that last line apart…

table+i moves from the first element of the array (which table points to) to the 1+ith. For example, when i =1, table+i points to the second element.

Because table+i is a pointer, we need to dereference it with the * operator before we try and assign a value to it. (This gets us to the actual int value pointed to.)

27

*(table+i) = 0;

28

Iterating over an array using only pointers

Thisdoesthesamethingastheprevioustwoexamples,butdoesn’tneedaloopcontrolvariable(i inthelastexample.)

28

for (ptr = my_arr; ptr < &my_arr[SIZE]; ptr++)*ptr = 0;

29

Key DifferenceThe one difference between an array name and a pointer is that the latter is a variable but the former is a constant. So ptr = array and ptr++ are valid operations, but constructs like array = ptr or array++ are illegal.

This is because an array name is a constant pointer, set at compile time. It’s not to be changed!

29


Recommended