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.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Programming and Data Structure
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Introduction to C Programming CE
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Lecture 5 Pointers 1. Variable, memory location, address, value
UNIT 5 C Pointers.
EPSII 59:006 Spring 2004.
Lecture 9: Pointers B Burlingame 25 October 2017.
Pointer.
Lecture 6 C++ Programming
Lecture 10: Strings B Burlingame 4 April 2018.
Pointers.
Topics discussed in this section:
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers  Week 10.
Pointers Call-by-Reference CSCI 230
5.1 Introduction Pointers Powerful, but difficult to master
Overloading functions
C Programming Lecture-8 Pointers and Memory Management
C Pointers Systems Programming.
Pointers and pointer applications
C Pointers Another ref:
Presentation transcript:

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  Open labs tonight and Tuesday  Due in lab, next week Final information & review next week

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

What is a Variable? Variables in general  Variable declaration informs compiler of two things: 1. Name of the variable 2. 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 0 intvar1 Var valueVar typeVar name Symbol Table Memory (8-bit) Address x10FE 0x10FF Bit x1100 0x1101

Bit Memory (8-bit) Address x10FE 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; 1100 short intj 10FE Address short inti ValueTypeName Symbol Table

So, what is a Pointer? 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" #include 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 for the 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.

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

Accessing What a Pointer Points To The indirection operator * gets the value at the address stored in the pointer #include 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; }

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?)

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; }

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

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 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]); }

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

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 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); }

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) ); }

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 ); }

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