Engineering Computing I Chapter 5 Pointers and Arrays.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Lectures 10 & 11.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Pointer A variable that represent the location (rather than value) of a data item, such as variable or an array element It is used to pass information.
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.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Pointers CSE 2451 Rong Shi.
Operator Overloading and Type Conversions
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
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.
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 A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Arrays and Pointers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Pointer Data Type and Pointer Variables
Homework Starting K&R Chapter 5 Good tutorial on pointers
Chapter 9: Pointers and String
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Presentation transcript:

Engineering Computing I Chapter 5 Pointers and Arrays

Pointers - Introduction A pointer is a variable that contains the address of a variable Pointers are much used in C, because: Pointers are sometimes the only way to express a computation Pointers usually lead to more compact and efficient code Pointers and arrays are closely related Spring 20112Chapter 5

Pointers - Introduction Block of Memory Pointer: Contains the address of c The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to Therefore the statement x = *p; and x = c; are equivalent Spring 20113Chapter 5

Pointers - Exercise Find the values of x, y, and z[0] through z[9] after the program ends Spring 20114Chapter 5

Pointers Manipulations This means the location that dp points to is a double x=x+10; y=x+1; x=x+1; ++x x++ Spring 20115Chapter 5

Pointers and Function Arguments Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function Swaps ONLY copies of the arguments! Spring 20116Chapter 5

Pointers and Function Arguments Passes addresses of the two operands to function Spring 20117Chapter 5

Pointers and Arrays Spring 20118Chapter 5

Pointers and Arrays *pa  a[0] *(pa+1)  a[1] *(pa+2)  a[2] Spring 20119Chapter 5

Pointers and Arrays Spring Chapter 5

Pointers and Arrays Spring Chapter 5

Pointers and Arrays Preferred Declaration Spring Chapter 5

Address Arithmetic If p is a pointer to some element of an array, then: p++ increments p to point to the next element p+=i increments it to point i elements beyond where it currently does. Spring Chapter 5

Address Arithmetic If p and q point to members of the same array, then: relations like ==, !=, =, etc., work properly For example, p < q is true if p points to an earlier element of the array than q does Spring Chapter 5

Character Pointers and Functions char *pmessage; pmessage = "now is the time"; assigns to pmessage a pointer to the character array. This is not a string copy; only pointers are involved. C does not provide any operators for processing an entire string of characters as a unit Spring Chapter 5

Character Pointers and Functions amessage is an array pmessage is a pointer Spring Chapter 5

Character Pointers and Functions Spring Chapter 5

Character Pointers and Functions Preferred Code! Spring Chapter 5

Pointer Arrays; Pointers to Pointers Spring Chapter 5

Multi-dimensional Arrays Spring Chapter 5

Multi-dimensional Arrays Write a function that multiplies two n by n square matrices together. Spring Chapter 5

Initialization of Pointer Arrays Spring Chapter 5

Pointers vs. Multi-dimensional Arrays  a is a true two-dimensional array: 200 int-sized locations have been set aside  The conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col] Spring Chapter 5

Pointers vs. Multi-dimensional Arrays  b is an array of 10 pointers which are not initialized  Assuming that each element of b does point to a twenty- element array, then there will be 200 ints set aside, plus ten cells for the pointers.  The important advantage of the pointer array is that the rows of the array may be of different lengths.  That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all! Spring Chapter 5

Pointers vs. Multi-dimensional Arrays Spring Chapter 5

Command-line Arguments Spring Chapter 5

Command-line Arguments Spring Chapter 5

Exercise TBA Spring Chapter 5