Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Lectures 10 & 11.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Topics discussed in this section:
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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. 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.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers Applications
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
[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.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
Topics discussed in this section:
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
Copyright © – Curt Hill Pointers A Light Introduction.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
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
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Introduction to Programming Using C
Pointers Introduction
Chapter 12 Enumerated, Structure, and Union Types Objectives
Introduction to the C Language
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
Chapter 9 Pointers Objectives
Pointer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to the C Language
Introduction to the C Language
Topics discussed in this section:
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Pointers.
Introduction to C++ Programming Language
Pointers Lecture 1 Thu, Jan 15, 2004.
Overloading functions
Pointers and Pointer-Based Strings
Pointers and pointer applications
C Pointers Another ref:
Presentation transcript:

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define, and initialize pointers ❏ To write programs that access data through pointers ❏ To use pointers as parameters and return types ❏ To understand pointer compatibility, especially regarding pointers to pointers ❏ To understand the role of quality in software engineering Chapter 9 Chapter 9 Pointers Pointers

Computer Science: A Structured Programming Approach Using C2 FIGURE 9-1 Derived Types

Computer Science: A Structured Programming Approach Using C3 9-1 Introduction A pointer is a constant or variable that contains an address that can be used to access data. Pointers are built on the basic concept of pointer constants. Pointer Constants Pointer Values Pointer Variables Accessing Variables Through Pointers Pointer Declaration and Definition Declaration versus Redirection Initialization of Pointer Variables Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C4 FIGURE 9-2 Character Constants and Variables

Computer Science: A Structured Programming Approach Using C5 FIGURE 9-3 Pointer Constants

Computer Science: A Structured Programming Approach Using C6 Pointer constants, drawn from the set of addresses for a computer, exist by themselves. We cannot change them; we can only use them. Note

Computer Science: A Structured Programming Approach Using C7 An address expression, one of the expression types in the unary expression category, consists of an ampersand (&) and a variable name. Note

Computer Science: A Structured Programming Approach Using C8 FIGURE 9-4 Print Character Addresses

Computer Science: A Structured Programming Approach Using C9 A variable’s address is the first byte occupied by the variable. Note

Computer Science: A Structured Programming Approach Using C10 FIGURE 9-5 Integer Constants and Variables

Computer Science: A Structured Programming Approach Using C11 FIGURE 9-6 Pointer Variable

Computer Science: A Structured Programming Approach Using C12 FIGURE 9-7 Multiple Pointers to a Variable

Computer Science: A Structured Programming Approach Using C13 A pointer that points to no variable contains the special null-pointer constant, NULL. Note

Computer Science: A Structured Programming Approach Using C14 An indirect expression, one of the expression types in the unary expression category, is coded with an asterisk (*) and an identifier. Note

Computer Science: A Structured Programming Approach Using C15 FIGURE 9-8 Accessing Variables Through Pointers

Computer Science: A Structured Programming Approach Using C16 FIGURE 9-9 Address and Indirection Operators

Computer Science: A Structured Programming Approach Using C17 FIGURE 9-10 Pointer Variable Declaration

Computer Science: A Structured Programming Approach Using C18 FIGURE 9-11 Declaring Pointer Variables

Computer Science: A Structured Programming Approach Using C19 PROGRAM 9-1Demonstrate Use of Pointers

Computer Science: A Structured Programming Approach Using C20 PROGRAM 9-1Demonstrate Use of Pointers

Computer Science: A Structured Programming Approach Using C21 FIGURE 9-12 Uninitialized Pointers

Computer Science: A Structured Programming Approach Using C22 FIGURE 9-13 Initializing Pointer Variables

Computer Science: A Structured Programming Approach Using C23 PROGRAM 9-2Fun with Pointers

Computer Science: A Structured Programming Approach Using C24 PROGRAM 9-2Fun with Pointers

Computer Science: A Structured Programming Approach Using C25 PROGRAM 9-2Fun with Pointers

Computer Science: A Structured Programming Approach Using C26 FIGURE 9-14 Add Two Numbers Using Pointers

Computer Science: A Structured Programming Approach Using C27 PROGRAM 9-3Add Two Numbers Using Pointers

Computer Science: A Structured Programming Approach Using C28 PROGRAM 9-3Add Two Numbers Using Pointers

Computer Science: A Structured Programming Approach Using C29 FIGURE 9-15 Demonstrate Pointer Flexibility

Computer Science: A Structured Programming Approach Using C30 PROGRAM 9-4Using One Pointer for Many Variables

Computer Science: A Structured Programming Approach Using C31 PROGRAM 9-4Using One Pointer for Many Variables

Computer Science: A Structured Programming Approach Using C32 FIGURE 9-16 One Variable with Many Pointers

Computer Science: A Structured Programming Approach Using C33 PROGRAM 9-5Using A Variable with Many Pointers

Computer Science: A Structured Programming Approach Using C34 PROGRAM 9-5Using A Variable with Many Pointers

Computer Science: A Structured Programming Approach Using C Pointers for Inter-function Communication One of the most useful applications of pointers is in functions. When we discussed functions in Chapter 4, we saw that C uses the pass-by-value for downward communication. For upward communication, we normally pass an address. In this section, we fully develop the bi-directional communication. Passing Addresses Functions Returning Pointers Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C36 FIGURE 9-17 An Unworkable Exchange

Computer Science: A Structured Programming Approach Using C37 FIGURE 9-18 Exchange Using Pointers

Computer Science: A Structured Programming Approach Using C38 Every time we want a called function to have access to a variable in the calling function, we pass the address of that variable to the called function and use the indirection operator to access it. Note

Computer Science: A Structured Programming Approach Using C39 FIGURE 9-19 Functions Returning Pointers

Computer Science: A Structured Programming Approach Using C40 It is a serious error to return a pointer to a local variable. Note

Computer Science: A Structured Programming Approach Using C Pointers to Pointers So far, all our pointers have been pointing directly to data. It is possible—and with advanced data structures often necessary—to use pointers that point to other pointers. For example, we can have a pointer pointing to a pointer to an integer.

Computer Science: A Structured Programming Approach Using C42 FIGURE 9-20 Pointers to Pointers

Computer Science: A Structured Programming Approach Using C43 FIGURE 9-21 Using Pointers to Pointers

Computer Science: A Structured Programming Approach Using C44 PROGRAM 9-6Using pointers to pointers

Computer Science: A Structured Programming Approach Using C45 PROGRAM 9-6Using pointers to pointers

Computer Science: A Structured Programming Approach Using C46 PROGRAM 9-6Using pointers to pointers

Computer Science: A Structured Programming Approach Using C Compatibility It is important to recognize that pointers have a type associated with them. They are not just pointer types, but rather are pointers to a specific type, such as character. Each pointer therefore takes on the attributes of the type to which it refers in addition to its own attributes. Pointer Size Compatibility Dereference Type Compatibility Dereference Level Compatibility Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C48 PROGRAM 9-7Demonstrate Size of Pointers

Computer Science: A Structured Programming Approach Using C49 PROGRAM 9-7Demonstrate Size of Pointers

Computer Science: A Structured Programming Approach Using C50 PROGRAM 9-7Demonstrate Size of Pointers

Computer Science: A Structured Programming Approach Using C51 FIGURE 9-22 Dereference Type Compatibility

Computer Science: A Structured Programming Approach Using C52 Pointers to void A pointer can be typed as void, e.g., void* pVoid The type void with pointers is a generic type that is not associated with a reference type. It is compatible, for assignment purposes, with all pointer types. Thus a pointer to void type can assigned to a pointer of any reference type and a pointer of any reference type can be assigned to a pointer of void type. However a pointer to void type cannot be deferenced unless it is cast. In a previous example we saw the statement pc = &a which was illegal since pc was a pointer to a char and a was type int. It can be made legal as follows: pc = (char*) &a although this is not recommended.

Computer Science: A Structured Programming Approach Using C53 Some more examples void* pVoid; char* pChar; int* pInt; pVoid = pChar; pInt = pVoid; pInt = (int*) pChar; The above statements are all legal but not recommended since other operations that use the casted pointer must also be casted or serious problems can arise.

Computer Science: A Structured Programming Approach Using C54 A void pointer cannot be dereferenced. Note

Computer Science: A Structured Programming Approach Using C55 FIGURE 9-23 Dereference Level Compatibility

Computer Science: A Structured Programming Approach Using C Lvalue and Rvalue In C, an expression is either an lvalue or an rvalue. As you know, every expression has a value. But the value in an expression (after evaluation) can be used in two different ways. Pointer Examples Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C57 Table 9-1lvalue Expressions

Computer Science: A Structured Programming Approach Using C58 The right operand of an assignment operator must be an rvalue expression. Note

Computer Science: A Structured Programming Approach Using C59 Table 9-2Operators That Require lvalue Expressions

Computer Science: A Structured Programming Approach Using C60 Table 9-3Invalid rvalue Expressions

Computer Science: A Structured Programming Approach Using C61 PROGRAM 9-8Convert Seconds to Hours, Minutes, and Seconds

Computer Science: A Structured Programming Approach Using C62 PROGRAM 9-8Convert Seconds to Hours, Minutes, and Seconds

Computer Science: A Structured Programming Approach Using C63 Create local variables when a value parameter will be changed within a function so that the original value will always be available for processing. Note

Computer Science: A Structured Programming Approach Using C64 When several values need to be sent back to the calling function, use address parameters for all of them. Do not return one value and use address Parameters for the others. Note

Computer Science: A Structured Programming Approach Using C65 FIGURE 9-24 A Common Program Design

Computer Science: A Structured Programming Approach Using C66 FIGURE 9-25 Using Pointers as Parameters

Computer Science: A Structured Programming Approach Using C67 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C68 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C69 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C70 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C71 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C72 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C73 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C74 PROGRAM 9-9Quadratic Roots

Computer Science: A Structured Programming Approach Using C Software Engineering In this chapter, we discuss a general software engineering topic, quality, which can be applied to any topic, including pointers. Quality Defined Quality Factors The Quality Circle Conclusion Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C76 Software that satisfies the user’s explicit and implicit requirements, is well documented, meets the operating standards of the organization, and runs efficiently on the hardware for which it was developed. Note

Computer Science: A Structured Programming Approach Using C77 FIGURE 9-26 Streams

Computer Science: A Structured Programming Approach Using C78 FIGURE 9-27 Streams