Docsity

Pointers and Arrays in C: Understanding Addresses and Values - Prof. James T. Sasaki, Study notes of Programming Languages

Partial preview of the text

Download Pointers and Arrays in C: Understanding Addresses and Values - Prof. James T. Sasaki and more Study notes Programming Languages in PDF only on Docsity!

CS 350

Computer Organization & Assembly

Language Programming

Spring 2009

Notes 21, 2009–04–

1

Chapter 16

Pointers & Arrays

Pointers and Arrays

• We've seen examples of both of these in our LC-3 programs; now

we'll see them in C.

• Pointer

- Address of a variable in memory

- Allows us to indirectly access variables

  • (^) in other words, we can talk about its address rather than its value

• Array

- A^ list of values arranged sequentially in memory

- Example: a list of telephone numbers

- Expression a[4] refers to the 5th element of the array a

3

Address vs. Value

• Sometimes we want to deal with the

address of a memory location, rather than the value it contains.

• Recall example from Chapter 6:

adding a column of numbers.

- R2 contains address of first location.

- Read value, add to sum, and

increment R2 until all numbers have been processed.

• R2 is a^ pointer^ — it contains the

address of data we’re interested in.

x

x

x

x

x

x

x11B

x

x

x

x

x

x

x

x

x

x

R

address

value

Pointers in C

• C lets us talk about and manipulate

pointers as variables and in expressions.

• Declaration

int p; / p is a pointer to an int */

• A^ pointer in C is always a pointer to a

particular data type:

int, double, char*, etc.

7

Pointers in C

• Pointer operators

*p — returns the value pointed to by p

&z — returns the address of variable z

In general, *&z is z

• No corresponding operators in Java

+ Java hides pointer use from programmer

+ Increases safety

Example

9 int i; Allocate space for an integer int *ptr; Allocate space for a pointer to an integer i = 4; Store the value 4 into the memory location associated with i ptr = &i; Store the address of i into the memory location associated with ptr *ptr = Store the result into memory at the address stored in ptr *ptr + 1; Read the contents of memory at the address stored in ptr i 4 x ptr

x don't care int i; int *ptr; i = 4; i 4 x ptr x ptr = &i; i 5 ptr *ptr = *ptr+1;

Passing Pointers to a

Function

main() wants to swap the values of valueA and valueB so it passes the addresses to NewSwap: NewSwap(&valueA, &valueB); Code for passing arguments: ADD R0, R5, #-1 ; addr of valueB ADD R6, R6, #-1 ; push STR R0, R6, # ADD R0, R5, #0 ; addr of valueA ADD R6, R6, #-1 ; push STR R0, R6, # tempVal firstVal secondVal valueB valueA

xEFFA

xEFF

xEFFD

R

R

13

Code Using Pointers

Inside the NewSwap routine ; int tempVal = *firstVal; LDR R0, R5, #4 ; R0=xEFFA LDR R1, R0, #0 ; R1=M[xEFFA]= STR R1, R5, #4 ; tempVal= ; *firstVal = *secondVal; LDR R1, R5, #5 ; R1=xEFF LDR R2, R1, #0 ; R1=M[xEFF9]= STR R2, R0, #0 ; M[xEFFA]= ; *secondVal = tempVal; LDR R2, R5, #0 ; R2= STR R2, R1, #0 ; M[xEFF9]=

tempVal

firstVal

secondVal

valueB

valueA

xEFFA

xEFF

xEFFD

R

R

Null Pointer

• Sometimes we want a pointer that points

to nothing.

• In other words, we declare a pointer, but

we’re not ready to actually point to

something yet.

int p; p = NULL; / p is a null pointer */ 15

Null Pointer

• NULL^ is a predefined macro that contains

a value that a non-null pointer should

never hold.

- Often (but not always),^ NULL^ =^0 , because

address 0 is not a legal address for most most

platforms.

Syntax for Pointer Declaration

• Declaring a pointer to some^ type^ of value

( type = int, char, etc).

type * var;!! e.g. int *p;

type * var;!! e.g.! int* p;

• Either of these work — whitespace

doesn't matter.

• Pointer variable has type^ int*^ (integer

pointer), char*, etc.

19

Declaring Multiple Variables

That Include Pointers

• It's okay to combine^ int^ *p;^ int^ *q;

into one declaration int *p, *q;

- But note that the asterisk has to be included

for both variables.

- Writing^ int*^ p,^ q;^ might make you think

that both p and q are pointers, but only p is;

q"isn't. (Spaces aren’t significant.)

int *p, plain_integer_variable;

Syntax for Creating a Pointer

Value

• To create a pointer value you take the

address of something

• Prefix operator^ &^ – use^ &var^ as an

expression.

+ Can apply & to anything that can appear on

the l.h.s. of an assignment. E.g. &var and

&a[i] are okay but &17 isn't.

int x = 17;

int p = &x; / not &17 */

21

Syntax for Dereferencing a

Pointer Value

• Dereferencing a pointer = follow the ptr.

- Can be applied to any expression that yields a

pointer.

- If^ p^ is a pointer variable (so it contains a

location) then *p refers to the location.

printf("p pts to loc with value %d\n", p); printf("p pts to loc %x\n", p); / is this ok? / printf("x is at loc %x\n", &x); / is this ok? */

C Code for IntDivide

int IntDivide(int x, int y, int *quoPtr, int remPtr) { if (y == 0) / Check for bad arguments */ return -1; quoPtr = x / y; / Put quotient in *quoPtr */ remPtr = x % y; / Put remainder in *remPtr */ return 0; } 25

Arrays

• Array declaration in C:

int num[4]; /* size = constant */

• Legal indexes/subscripts start at 0

num[0] = 5;

num[3] = num[0]*

num[4] = 17; /* error */

• No runtime check^ for out-of-bounds

index.

Local Array Variables

• Array elements are allocated

as part of the activation record.

int grid[10];

• First element^ grid[0]^ is at

lowest address of allocated space.

• If^ grid^ is first var allocated,

then R5 will point to grid[9].

grid[0]

grid[1]

grid[2]

grid[3]

grid[4]

grid[5]

grid[6]

grid[7]

grid[8]

grid[9]