Topics discussed in this section:

Slides:



Advertisements
Similar presentations
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Advertisements

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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
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: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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
Computer Science 210 Computer Organization Pointers.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Computer Science: A Structured Programming Approach Using C Masks In many programs, bits are used as binary flags: 0 is off, and 1 is on. To set.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
[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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 02 (Part II) Introduction to C++ Programming.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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 7 Text Input/Output Objectives
Topics discussed in this section:
Chapter 12 Enumerated, Structure, and Union Types Objectives
Introduction to the C Language
FIGURE 9-5 Integer Constants and Variables
Topics discussed in this section:
Chapter 9 Pointers Objectives
Pointer.
Using local variable without initialization is an error.
Introduction to the C Language
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Structure of a C Program
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.
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.
Topics discussed in this section:
Pointers.
Topics discussed in this section:
Introduction to C++ Programming Language
Topics discussed in this section:
Topics discussed in this section:
Pointers Pointers point to memory locations
C++ Programming Lecture 17 Pointers – Part I
Operator Overloading; String and Array Objects
Topics discussed in this section:
Pointers and pointer applications
Topics discussed in this section:
C Pointers Another ref:
Presentation transcript:

Topics discussed in this section: 9-2 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. Topics discussed in this section: Passing Addresses Functions Returning Pointers Computer Science: A Structured Programming Approach Using C

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

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

Note 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. Computer Science: A Structured Programming Approach Using C

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

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

9-3 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 C

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

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

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

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

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

Topics discussed in this section: 9-4 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. Topics discussed in this section: Pointer Size Compatibility Dereference Type Compatibility Dereference Level Compatibility Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

Topics discussed in this section: 9-5 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. Topics discussed in this section: Pointer Examples Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

Do not return one value and use address Parameters for the others. Note 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. Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

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

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

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

Note 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. Computer Science: A Structured Programming Approach Using C

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

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