lecture 12: pointers b burlingame 25 nov 2015. announcements homework 6 due homework 7 posted, due...

17
Lecture 12: Pointers B Burlingame 25 Nov 2015

Upload: clemence-walsh

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Lecture 12: Pointers

B Burlingame

25 Nov 2015

Page 2: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Announcements Homework 6 due Homework 7 posted, due with the final

Final prep Take home Lab posted tonight

Open labs tonight and Tuesday Due in lab, next week

Final information & review next week

Page 3: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Languages

There are many programming languages C, C++, Perl, Java, etc.

C C originates in 1972 Co-developed with UNIX

Dennis Ritchie, Brian Kernighan, Ken Thompson K&R C is still a thing

Why does C continue? Pointer variables

Page 4: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

What is a Variable? Variables in general

Variable declaration informs compiler of two things:

1. Name of the variable2. Data type of the variable Actions caused:

Bytes allocated in memory Symbol table with name,

address, and value Can think of a variable as

having two "values" Value of what is stored at the

memory location (rvalue) Value of the memory location

(its address) (lvalue)

int var1 = 0;

10FE

Var address

0intvar1

Var valueVar typeVar name

Symbol Table

Memory (8-bit)Address

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0x10FE

0x10FF

Bit 7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0x1100

0x1101

Page 5: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Bit 7 6 5 4 3 2 1 0

Memory (8-bit)Address

0 0 0 0 0 0 0 0

0 0 0 0 0 0 1 1

0x10FE

0x10FF

What is a Variable? Variables in general,

cont. Assigning a value to a

variable (i = 3;) Value is copied to the

memory location at the address listed in the symbol table

Using a variable in an expression (j = i;)

Value of what is stored at the memory location is accessed

short int i=0, j=0;

i = 3; j = i;

1100short intj

10FE

Address

short inti

ValueTypeName

Symbol Table

Page 6: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

So, what is a Pointer? Pointer variable

A variable thatcontains anaddress

Declaring apointervariable

type * varname int* ptr1; float *ptr2; char * ptr3;

Read as: "ptr1 is a pointer to an integer" "ptr2 is a pointer to a float" "ptr3 is a pointer to a char"

#include <stdio.h>

int main(){ int num1 = 0; int *ptr1 = &num1, *ptr2 = NULL; num1 = 7; printf("size of num1: %d\n",sizeof(num1)); printf("value of num1: %d\n",num1); printf("address of num1: %p\n",&num1); printf("address in ptr1: %p\n",ptr1); return 0;}

&num1 means:the address forthe variable num1

• What is the size of num1?• Is &num1 == ptr1?• What address is stored in ptr2?

Always initialize pointers you declare!An uninitialized pointer is like a loaded gun pointed in a direction that you don’t know.

Page 7: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Why Use Pointers? Allows a function to modify variables in the

calling function Recall: every parameter in a function call implies a

memory allocation of a new variable and then the copy of the value of that parameter

Return more than one value from a function call

Pass a pointer to a large data structure rather than copying the whole structure

Directly access hardware Memory mapped hardware is very common

Page 8: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Accessing What a Pointer Points To

The indirection operator * gets the value at the address stored in the pointer

#include <stdio.h>

int main(){ int num1 = 0; int *ptr1 = &num1; int *ptr2 = NULL;

ptr2 = ptr1; num1 = 7;

printf("value of num1: %d\n", num1); printf("value of num1: %d\n", *ptr1); printf("value of num1: %d\n", *ptr2); return 0;}

Page 9: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Pointer - Practice 1

Declare: x_ptr, a pointer to an integer y_ptr, a pointer to a double

What do the following statements do? char *my_ptr, my_char1 = 'c', my_char2; my_ptr = &my_char1; my_char2 = *my_ptr;

What is in: my_ptr (in declaration? in second line?) my_char1 my_char2 (in declaration? in last line?)

Page 10: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Example: swap

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

int temp = *a;*a = *b;*b = temp;

}

• Need to exchange the values in two variables in the calling function• Note the use of asterisks (*) and ampersands (&)

int main (void){

int dove = 5, eagle = 17;swap( &dove, &eagle );return 0;

}

Page 11: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Recall: what is an array?

Index No. [0] [1] [2] [3 [4] [5] [6] [7] [8] [9]int nums [10];

Element No. 1 2 3 4 5 6 7 8 9 10

int nums [10]; 10 element array of integers

Element no. 3 is accessed by:nums [2]

because indexing begins at 0

Page 12: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Passing an Array to a Function Prototype and

function header need:

data type array name [ ]

Function call with actual name of array

Note: in the function prototype and function header

char *array would also work

#include <stdio.h>void PrintArray(int elements, char array[]);int main(){

/* initialize the array */ char test[]={'M','E','3','0'};

/* get the size of the array */int num_elem=sizeof(test)/sizeof(char);

/* pass array to function */

PrintArray(num_elem, test); return 0;}/* PrintArray() function definition */void PrintArray(int num_elem, char array[]){ int i=0; for(i=0; i<num_elem; i++)

{ printf("test[%d]==%c",i,array[i]); printf(" @ 0x%p\n",&array[i]); }}

Page 13: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

How do pointers and arrays relate?

Index No. [0] [1] [2] [3 [4] [5] [6] [7] [8] [9]int nums [10];

Element No. 1 2 3 4 5 6 7 8 9 10Offset +0 +1 +2 +3 +4 +5 +6 +7 +8 +9

Address 0x0 0x4 0x8 0xC 0x10 0x14 0x18 0x1C 0c20 0x24

int nums [10]; 10 element array of integers

Element no. 3 is accessed by: nums [2]- or -

Element no. 3 can be access by: *(num + 2)

Where num is the head address of the array and 2 is the offset

Page 14: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Passing an Array Pointer to a Function

Prototype and function header need:

data type array name

(as pointer) Function call

with actual name of array

#include <stdio.h>void PrintArray(int elements, char *array);int main(){

/* initialize the array */ char test[]={'M','E','3','0'};

/* get the size of the array */int num_elem=sizeof(test)/sizeof(char);

/* pass array to function */

PrintArray(num_elem, test); return 0;}/* PrintArray() function definition */void PrintArray(int num_elem, char *array){ int i=0; for(i=0; i<num_elem; i++) { printf("test[%d]==%c",i, *(array + 1)); printf(" @ 0x%p\n", array + 1); }}

Page 15: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Pointers and Arrays An array is basically a static pointer to the

head (element 0) of an arrayvoid show_array( int x[], int *y, int size );

int main( void ){ int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //since arrays are already storing addresses, no & necessary show_array( a, a, 10 ); return 0;}

void show_array( int x[], int *y, int size ){ int i = 0; for( i = 0; i < size; ++i ) { //note how the array and pointer notation looks printf( "%d\t%d\n", x[i], *(y + i) ); }}

Page 16: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

Pointers and Arrays An array is basically a static pointer to the

head (element 0) of an arrayvoid show_elem( int *x, int *y );

int main( void ){ int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //an element of an array, still stores a value; therefore an & is

//necessary. Using the pointer offset method, however, doesn’t

show_elem( a + 3, &a[3] ); return 0;}

void show_elem( int *x, int *y ){

printf( “%d\t%d\n”, *x, *y );}

Page 17: Lecture 12: Pointers B Burlingame 25 Nov 2015. Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight

References Darnell, P. A. & Margolis, P. E. (1996) C, a

software engineering approach, 3rd ed., Springer, New York, p. 327.

http://www.cppreference.com/wiki/c/io/fopen, Visited 23OCT2010.

http://en.wikipedia.org/wiki/Standard_streams, Visited 23OCT2010.