CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Programming and Data Structure
Pointers in C Rohit Khokher
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
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:
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Pointers and Pointer-Based Strings
Pointer.
Pointers and References
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
Overloading functions
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Data Structures and Algorithms Introduction to Pointers
Standard Version of Starting Out with C++, 4th Edition
Introduction to Pointers
Presentation transcript:

CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel

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. x = 1.0; intarray[2] = 17; Constants and expressions are not lvalues

Memory and Data Representation Every lvalue is stored somewhere in memory and therefore has an address Once allocated, the address of an lvalue never changes, even though its contents may change Depending on type of data, different lvalues require different amounts of memory The address of an lvalue is itself data that can be manipulated and stored in memory

Array Arguments Creating a function that operates on an array is declaring a variable to contain an l-value Prototype: void f(int array[]); array is initialized with the memory address of the array passed in f(my_array); array then contains the address of an integer and using array[i] calculates an offset from this address

Pointer Declaration A pointer is a variable storing the address of another variable Declaration: type *ptr; Example: int *p1, *p2; p1 and p2 are referred to as pointers-to-int, and may contain the addresses of integers

Pointer Operations C++ defines two operators that manipulate pointer values: &address-of *value-pointed-to (dereference) Operand of & must be lvalue, and & returns the address of the lvalue Operand of * must be a pointer, and * returns the value pointed to by the pointer

Pass by Reference To modify value of argument, use pointer void SetToZero(int *ip) { *ip = 0; } which makes “ip” a pointer to the location of the value of the argument of the call SetToZero(&x);

Advantage of use of & operator When calling a function using func(x); you know the value of x will not change When calling a function using func(&x); the value of x may be changed by the function Easier to predict effects of function call

SwapInteger function When sorting arrays, we wanted to use SwapIntegers(array[low], array[high]); Using pointers allows the following: void SwapIntegers(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } and SwapIntegers(&array[low], &array[high]);

Returning Multiple Results Convert time (minutes) to hours and minutes void Convert(int time, int *hours, int *mins) { *hours = time / 60; *mins = time % 60; } Call with ConvertTime(t, &h, &m);

Array Review Array elements are lvalues with addresses double list[3]; Three consecutive memory units, each 8 bytes long Each element has address, retrieved as &list[i] Address determined by &list[i]- base + i * 8

Pointer Arithmetic Addition and subtraction of pointers Fundamental insight of pointer arithmetic: –If p is a pointer to the initial element of an array arr, and k is an integer, then p + k is defined to be &arr[k] That is, adding an integer k to a pointer to an integer adds k * sizeof(int) to pointer

More Pointer Arithmetic If two pointers to integer p1 and p2 and an integer k have the relationship p2 == p1 + k Then p2 is exactly k * sizeof(int) larger than p1 and the result of subtracting the pointers p2 - p1 is the integer k

Recall ++ and -- Recall: x++ calculates value of x first and increments it. Expression’s value is value before increment. ++x increments value of x first and then uses new value as value of operation as a whole.

Incrementing pointers Consider the expression ( p is a pointer to int) *p++ Since unary operators are right-to-left order, thus it is equivalent to *(p++) and dereferences the pointer and returns the object to which it points, but, as a side effect, point p to the next integer

Pointers and Arrays The declaration int arr[5]; allocates space for an array of five integers The following identity holds arr  &arr[0] The function prototypes are equivalent double Average(int array[], int n); double Average(int *array, int n);

Proper Declaration Arrays and pointers may be used interchangeably General rule: –declare parameters to reflect their usage If using arrays, declare arrays If using pointers, declare pointers

Pointer/Array Difference The declaration int arr[5]; reserves memory for five integers The declaration int *p; only reserves memory for one pointer Declaring an array gives storage to work with Declaring pointer only lets you manipulate existing storage

Dynamic Memory Allocation Can use pointer to access additional memory (variables) and release it p1 = new int; p2 = new int[100]; Now can use pointers to access memory *p1 = 0; cout << p2[i]; –Can treat p2 as if it were the name of an array To release, use delete p1; delete []p2;

Special pointer NULL NULL is the value of a pointer that does not currently point to any data (usually value 0) Can not dereference a NULL pointer Used to see if allocate successful p2 = new int[100]; if (p2 == NULL) // error allocating