Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9: Pointers B Burlingame 25 October 2017.

Similar presentations


Presentation on theme: "Lecture 9: Pointers B Burlingame 25 October 2017."— Presentation transcript:

1 Lecture 9: Pointers B Burlingame 25 October 2017

2 Announcements Homework Midterm grades in process
Remaining homework can be done in pairs, turn in one paper with both names Homework #4 due up front Homework #5 due in two weeks Midterm grades in process

3 Languages There are many programming languages C
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 Chapter 10 in the text

4 What is a Variable? Variables in general
int var1 = 0; Variables in general Variable declaration informs compiler of two things: Name of the variable 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) Memory (8-bit) Address 0x10FE 0x10FF Bit 7 6 5 4 3 2 1 0x1100 0x1101 Point out that declaration of an int allocates four bytes on the PC, and two bytes on a microcontroller. Big-endian: MSB is in the lowest memory address, LSB in the highest. Little-endian has the situation reversed. 10FE Var address int var1 Var value Var type Var name Symbol Table

5 What is a Variable? Variables in general, cont.
short int i=0, j=0; i = 3; j = i; 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 Bit 7 6 5 4 3 2 1 Memory (8-bit) Address 0x10FE 0x10FF A short int will designate 2 bytes of memory. When we assign a value to a variable, the variable is interpreted as its address. When we us a variable in an expression, the variable is interpreted as the value at its address. 1100 short int j 10FE Address i Value Type Name Symbol Table

6 So, what is a Pointer? Pointer variable Declaring a pointer variable
&num1 means: the address for the variable num1 #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; } Pointer variable A variable that contains an address Declaring a pointer variable 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" What is the size of num1? Is &num1 == ptr1? What address is stored in ptr2? A short int will designate 2 bytes of memory. Run the program pointer_practice1.c to show how pointers work Notes whitespace is not important in declaration. We can initialize a pointer when we declare it, just like we can initialize a variable when we declare it. Note however that we need to initialize a pointer with an address &varname refers to the address of varname The format specification for printing a pointer is %p ptr2 points to NULL (Ch is ‘nice’ to do that for you). NULL is a macro defined so that if we assigned ptrx = NULL, we are guaranteed that ptrx won't point to any object or function. We can do serious damage if we point to a memory location and wipe out something important! Good example and explanation of dereferencing an uninitialized pointer Always initialize pointers you declare! An uninitialized pointer is like a loaded gun pointed in a direction that you don’t know.

7 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

8 Accessing What a Pointer Points To
#include <stdio.h> int main() { int num1 = 0; int *ptr1 = &num1; int *ptr2 = NULL; ptr2 = ptr1; num1 = 7; *ptr1 = 8; printf("value of num1: %d\n", num1); printf("value of num1: %d\n", *ptr1); printf("value of num1: %d\n", *ptr2); return 0; } The indirection operator * gets the value at the address stored in the pointer Go to ChIDE and run pointer_practice2.c

9 Pointer - Practice 1 Declare: What do the following statements do?
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?) See pointer_practice3.c nul is not the same as NULL. nul refers to a zero as defined by the escape sequence \0, which means it occupies one byte of memory. NULL is a macro that is used to initialize null pointers.

10 Pointers and functions
Functions pass copies of their arguments into the function To get multiple values returned Use pointers in the argument list of the prototype and function definition Use & in front of variable names associated with pointer arguments in function call

11 Functions Returning Multiple Values
#include <stdio.h> void prod_sum(double x, double y, double *ptr1, double *ptr2); int main() { double var1 = 3.0, var2 = 5.0; double prod = 0, sum = 0; prod_sum(var1, var2, &prod, &sum); printf("var1= %g\n" "var2= %g\n",var1, var2); printf("prod= %g\n" "sum= %g\n", prod, sum); } /* function definition */ void prod_sum(double A, double B, double *rslt_prod, double *rslt_sum) *rslt_prod = A * B; *rslt_sum = A + B; Instead of product() prod_sum() How can I get both product and sum returned? put * in front of variable name in prototype and function definition put & in front of variable names in function call Called 'Pass by Reference'

12 Recall: what is an array?
int nums [10]; 10 element array of integers Element no. 3 is accessed by: nums [2] because indexing begins at 0

13 Passing an Array to a Function
#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]); 0x%p\n",&array[i]); 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 See print_array.c

14 How do pointers and arrays relate?
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

15 Passing an Array Pointer to a Function
#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)); 0x%p\n", array + 1); Prototype and function header need: data type array name (as pointer) Function call with actual name of array See print_array.c

16 Pointers and Arrays An array is basically a static pointer to the head (element 0) of an array void 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) ); See pointer_practice3.c nul is not the same as NULL. nul refers to a zero as defined by the escape sequence \0, which means it occupies one byte of memory. NULL is a macro that is used to initialize null pointers.

17 Pointers and Arrays An array is basically a static pointer to the head (element 0) of an array void 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 ); See pointer_practice3.c nul is not the same as NULL. nul refers to a zero as defined by the escape sequence \0, which means it occupies one byte of memory. NULL is a macro that is used to initialize null pointers.

18 References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3rd ed., Springer, New York, p. 327.


Download ppt "Lecture 9: Pointers B Burlingame 25 October 2017."

Similar presentations


Ads by Google