chapter 16 pointers and arrays

21
Chapter 16 Pointers and Arrays

Upload: tamara-robles

Post on 30-Dec-2015

64 views

Category:

Documents


8 download

DESCRIPTION

Chapter 16 Pointers and 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 Array A list of values arranged sequentially in memory - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 16 Pointers and Arrays

Chapter 16Pointers and Arrays

Page 2: Chapter 16 Pointers and Arrays

16-2

Pointers and ArraysWe've seen examples of both of thesein our LC-3 programs; now we'll see them in C.

Pointer• Address of a variable in memory• Allows us to indirectly access variables

Array• A list of values arranged sequentially in memory• The name of the array is a pointer to where the array begins

Page 3: Chapter 16 Pointers and Arrays

16-3

Address vs. ValueSometimes we want to deal with the addressof a memory location,rather than the value it contains.

Recall example from Asgn2:adding a column of numbers.• R2 contains address of first location.• Read value, add to sum, and

increment R2 until all numbershave been processed.

R2 is a pointer -- it contains theaddress of data we’re interested in.

x3107x2819x0110x0310x0100x1110x11B1x0019

x3100

x3101

x3102

x3103

x3104

x3105

x3106

x3107

x3100R2

address

value

Page 4: Chapter 16 Pointers and Arrays

16-4

Another Need for AddressesConsider the following function that's supposed toswap the values of its arguments.

void Swap(int first, int second){ int temp = first; first = second; second = temp;}

Does this work?

Page 5: Chapter 16 Pointers and Arrays

16-5

Pointers in CC lets us talk about and manipulate pointersas 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. (We’ll see why later)

Operators

*p -- returns the value pointed to by p

&z -- returns the address of variable z

Page 6: Chapter 16 Pointers and Arrays

16-6

Null PointerSometimes we want a pointer that points to nothing.

In other words, we declare a pointer, but we’re not readyto actually point to something yet.

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

NULL is a predefined macro that contains a value thata non-null pointer should never hold.

• Often, NULL = 0, because Address 0 is not a legal addressfor most programs on most platforms.

Page 7: Chapter 16 Pointers and Arrays

16-7

Using Arguments for Results

Pass address of variable where you want result stored

This solves the mystery of why ‘&’ with argument to scanf:

scanf("%d", &data);

read a decimal integerand store in data

Page 8: Chapter 16 Pointers and Arrays

16-8

Example using PointersIntDivide performs both integer division and remainder,returning results via pointers. (Returns –1 if divide by zero.)

int IntDivide(int x, int y, int *quoPtr, int *remPtr);

main(){

int dividend, divisor; /* numbers for divide op */ int quotient, remainer; /* results */ int error; /* ...code for dividend, divisor input removed... */ error = IntDivide(dividend, divisor, &quotient, &remainder); /* ...remaining code removed... */}

Page 9: Chapter 16 Pointers and Arrays

16-9

C Code for IntDivide

int IntDivide(int x, int y, int *quoPtr, int *remPtr){ if (y != 0) { *quoPtr = x / y; /* quotient in *quoPtr */ *remPtr = x % y; /* remainder in *remPtr */ return 0; } else return –1;}

Page 10: Chapter 16 Pointers and Arrays

16-10

Array SyntaxDeclaration

type variable[num_elements];

Array Reference

variable[index];

all array elementsare of the same type

number of elements must beknown at compile-time

Why?

i-th element of array (starting with zero);no limit checking at compile-time or run-time

Page 11: Chapter 16 Pointers and Arrays

16-11

Array as a Local VariableArray elements are allocatedas part of the activation record.

int x, y;int grid[10];

int z;

First element (grid[0])is at lowest addressof allocated space.

zgrid[0]grid[1]grid[2]grid[3]grid[4]grid[5]grid[6]grid[7]grid[8]grid[9]yx

R5

R6

Page 12: Chapter 16 Pointers and Arrays

16-12

ADD R0, R5, #-11 ; R0 = grid

; z = grid[3] + 1LDR R1, R0, #3 ; R1 = grid[3] ADD R1, R1, #1 ; plus 1STR R1, R0, #-1 ; a = R1

; grid[6] = 5;AND R2, R2, #0ADD R2, R2, #5 ; R2 = 5STR R2, R0, #6 ; grid[6] = R2

zgrid[0]grid[1]grid[2]grid[3]grid[4]grid[5]grid[6]grid[7]grid[8]grid[9]yx

R5

R6

R0

Page 13: Chapter 16 Pointers and Arrays

16-13

Passing Arrays as ArgumentsC passes arrays by reference

• the address of the array (i.e., of the first element)is written to the function's activation record

• otherwise, would have to copy each element

main() {int numbers[MAX_NUMS];…mean = Average(numbers, MAX_NUMS);…

}/* Average can be in another file, works on ANY sized array */

int Average(int vals[], int size) {…for (index = 0; index < size; index++)

sum = sum + vals[index];return (sum / size);

}

This must be a constant, e.g.,#define MAX_NUMS 10

Page 14: Chapter 16 Pointers and Arrays

16-14

A String is a Null Terminated Array of CharactersAllocate space for a string just like any other array:

char outputString[16];

Space for string must contain room for terminating zero.

Special syntax for initializing a string:

char outputString[16] = "Result = ";

…which is the same as:

outputString[0] = 'R';outputString[1] = 'e';outputString[2] = 's';...

outputString[9] = ‘\0’; (or 0 or NULL)

Page 15: Chapter 16 Pointers and Arrays

16-15

I/O with StringsPrintf and scanf use "%s" format character for string

Printf -- print characters up to terminating zero

printf("%s", outputString);

Scanf -- read characters until whitespace,store result in string, and terminate with zero

scanf("%s", inputString);

Page 16: Chapter 16 Pointers and Arrays

16-16

Relationship between Arrays and PointersAn array name is essentially a pointerto the first element in the array

char word[10];char *cptr;

cptr = word; /* points to word[0] */

Difference:Can change the contents of cptr, as in

cptr = &someString;

cptr = cptr + 1;

(The identifier "word" is not a variable.)

Page 17: Chapter 16 Pointers and Arrays

16-17

Correspondence between Ptr and Array NotationGiven the declarations on the previous page,each line below gives three equivalent expressions:

cptr word &word[0] &cptr[0]

(cptr + 3) word + 3 &word[3] &cptr[3]

*cptr *word word[0] cptr[0]

*(cptr + 3) *(word + 3) word[3] cptr[3]

Page 18: Chapter 16 Pointers and Arrays

16-18

Common Pitfalls with Arrays in COverrun array limits

• There is no checking at run-time or compile-timeto see whether reference is within array bounds.

int i; int array[10];

for (i = 0; i <= 10; i++) array[i] = 0;

Declaration with variable size• Size of array must be known at compile time.

void SomeFunction(int num_elements) { int temp[num_elements]; (can’t do this!) …}

Solutions (more on this later):int *temp = (int *)

malloc(sizeof(int)*num_elements);

Page 19: Chapter 16 Pointers and Arrays

16-19

Pointer ArithmeticAddress calculations depend on size of elements

• In our LC-3 code, we've been assuming one word per element.e.g., to find 4th element, we add 4 to base address

• It's ok, because we've only shown code for int and char,both of which take up one word.

• If double, we'd have to add 8 to find address of 4th element.

C does size calculations under the covers,depending on size of item being pointed to:

double x[10];

double *y = x;*(y + 3) = 13;

allocates 80 bytes (8 per element)

same as x[3] -- base address plus 24

Page 20: Chapter 16 Pointers and Arrays

16-20

Using strings in C#include <string.h>

int strlen(char *);

char *strcpy(char *dest, char *src);char *strncpy(char *dest, char *src, int maxlen);

int strcmp(char *a, char *b);• <0 if a before b in alphabetical order• 0 if string a is same as string b• >0 if a after b in alphabetical order

char *strtok(char *a, char *tokens);• Place a ‘\0’ at first occurrence of a token, return a

char *strtok(NULL, char *tokens);• Starting where it left off, divide at token, return next portion

type “man strlen” for more details

Page 21: Chapter 16 Pointers and Arrays

16-21

int strlen(char *s)Treated as an array:int strlen(char s[]){

int length = 0;while (s[length] != ‘\0’) length++;return length;

}Treated as a pointer:int strlen(char *s){

int length = 0;while (*(s++) != ‘\0’) { length++;}return length;

}