Pointers Ethan Cerami Fundamentals of Computer New York University.

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
1 CSE1301 Computer Programming Lecture 16 Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Topic 8 – Introduction to Pointers and Function Output Parameters.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Science 210 Computer Organization Pointers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture 17: The Last Puzzle Piece with Functions.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
UNIT 8 Pointers.
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
CS1010 Programming Methodology
CS1010 Programming Methodology
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
Functions and Pointers
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers Psst… over there.
CSI-121 Structured Programming Language Lecture 16 Pointers
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Basic notes on pointers in C
Computer Science 210 Computer Organization
Pointers Psst… over there.
Systems Programming Concepts
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Parameter Passing in Java
Simulating Reference Parameters in C
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
C Programming Lecture-8 Pointers and Memory Management
CISC181 Introduction to Computer Science Dr
C Pointers Systems Programming.
Presentation transcript:

Pointers Ethan Cerami Fundamentals of Computer New York University

Today n Computer Architecture n What’s a Pointer? n Declaring Pointers n Pointer Operators u & Address Operator u * Indirection Operator n Pointers and Call by Reference

Computer Architecture CPU: “Brains” of the Computer RAM: Short-term memory When you run your computer, RAM stores data for your operating system and all the applications you are running.

RAM (Random Access Memory) n Every spot in memory has a unique address. n Declaring Variables: When you declare a variable, the operating system allocates a specific spot in memory for that variable. n For example, suppose you have int x = 5; int x is placed at location 1005.

I. What’s a Pointer? n Pointer: contains the address of another variable. n For example, you could have two variables: x, xPtr. n Here, we say that xPtr “points to” x. n Or xPtr contains the address of x. x 5 xPtr

II. Declaring Pointers n data_type *var_name; n Pointers can point to any data type: int, char, float, double, long, etc. n Examples: u int *xPtr; F “xPtr is a pointer to type int.” u float *salesPtr; F “salesPtr is a pointer to type float.” * Indicates that this is a pointer.

III. Pointer Operators n Now we know how to declare pointers, but to use them you must understand the two pointer operators: n & Address Operator. Returns the address of a variable. n * Indirection Operator. Returns the value referenced by a pointer.

Pointer Operators in Action n int x = 5; n int *xPtr = NULL; xPtr points to Nothing x is located at address: 2 5 xPtr is located at address: 0 NULL

Using the Address & Operator n Now, you can apply the Address & operator. n &x Determines the address of x xPtr = &x; xPtr now points to x xxPtr 5 2

Using the * Indirection Operator n If you want to determine the value referenced by a pointer, you apply the * or indirection operator. n printf (“%d”, *xPtr);This prints “5” xxPtr 5 2

Example main() { int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */ a = 7; aPtr = &a; /* aPtr set to address of a */ printf("The address of a is %p\n", &a); printf("The value of aPtr is %p\n\n", aPtr); printf("The value of a is %d\n", a); printf("The value of *aPtr is %d\n\n", *aPtr); return 0; } %p is the address format specifier. Formats large numbers in Hexadecimal format.

Example (Cont.) n aPtr “points” to a. aPtra 7 Program Output: The address of a is 4F67:2240 The value of aPtr is 4F67:2240 The value of a is 7 The value of *aPtr is 7 These are Hexadecimal Values. %p is the format specifier for Pointer Addresses

IV. Call by Reference n Call by Value: u When you pass a variable, you pass a copy of the variable. u Changes to the copy do not affect the original value. n Call by Reference: u When you pass a variable, you pass a reference (pointer) to the original variable. u Changes to the reference (pointer) do affect the original variable.

Limitations of Call by Value n You can only return a single value from a function. n So, what happens if you want to return two values, three values, more? n The only way around this is to use pointers.

Example n Suppose you want to create a simple function that simply swaps two numbers. n For example, you give it the numbers: 4,5. And the function returns 5,4. n This is particularly useful for sorting programs, such as Bubble Sort. n Only problem: there is no way to do this with Call by Value, because you need to modify and return two values.

Pointers and Call by Reference n To get around this problem, we use pointers. void swap(int *element1Ptr, int *element2Ptr) { int temp; temp = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = temp; } * Indicates that we are passing pointers or addresses.

Swapping with Pointers n To use swap in a program: main () { int x = 5; int y = 4; printf ("Original Order: %d,%d\n", x,y); swap (&x, &y); printf ("After Swapping: %d,%d", x,y); } Instead of passing the actual values, we pass the addresses of the variables.

Swapping in Action n Within main: swap (&x, &y); n within swap: u element1Ptr now points to x u element2Ptr now points to y u temp = *element1Ptr; sets temp =5; u *element1Ptr = *element2Ptr;sets x = 4; u *element2Ptr = temp;sets y = 5; x 5 element1Ptry 4 element2Ptr Before Swap:

Swapping Program Output Original Order: 4,5 After Swapping: 5,4