Pointers Introduction

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Data Structures Using C++ 2E
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
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.
Pointers Applications
Computer Science 210 Computer Organization Pointers.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
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.
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.
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.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Topics discussed in this section:
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.
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
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.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 5 Pointers 1. Variable, memory location, address, value
Chapter 8 Arrays, Strings and Pointers
Introduction to Programming Using C
UNIT 5 C Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
Chapter 9 Pointers Objectives
Pointer.
Pointers Psst… over there.
Pointers and References
Pointers Psst… over there.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Topics discussed in this section:
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Topics discussed in this section:
Pointers.
Introduction to C++ Programming Language
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Lecture 1 Thu, Jan 15, 2004.
Overloading functions
Pointers Pointers point to memory locations
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Topics discussed in this section:
CISC181 Introduction to Computer Science Dr
Pointers and pointer applications
C Pointers Another ref:
Introduction to Pointers
Presentation transcript:

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

Character Constants and Variables Computer Science: A Structured Programming Approach Using C

Pointer Constants Computer Science: A Structured Programming Approach Using C

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

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

Print Character Addresses Computer Science: A Structured Programming Approach Using C

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

Integer Constants and Variables Computer Science: A Structured Programming Approach Using C

Pointer Variable Computer Science: A Structured Programming Approach Using C

Multiple Pointers to a Variable Computer Science: A Structured Programming Approach Using C

Note A pointer that points to no variable contains the special null-pointer constant, NULL. An indirect expression, one of the expression types in the unary expression category, is coded with an asterisk (*) and an identifier. Computer Science: A Structured Programming Approach Using C

Accessing Variables Through Pointers Computer Science: A Structured Programming Approach Using C

Address and Indirection Operators Computer Science: A Structured Programming Approach Using C

Pointer Variable Declaration Computer Science: A Structured Programming Approach Using C

Declaring Pointer Variables Computer Science: A Structured Programming Approach Using C

Demonstrate Use of Pointers Computer Science: A Structured Programming Approach Using C

Demonstrate Use of Pointers Computer Science: A Structured Programming Approach Using C

Uninitialized Pointers Computer Science: A Structured Programming Approach Using C

Initializing Pointer Variables Computer Science: A Structured Programming Approach Using C

Fun with Pointers Computer Science: A Structured Programming Approach Using C

Fun with Pointers Computer Science: A Structured Programming Approach Using C

Fun with Pointers Computer Science: A Structured Programming Approach Using C

Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

Demonstrate Pointer Flexibility Computer Science: A Structured Programming Approach Using C

Using One Pointer for Many Variables Computer Science: A Structured Programming Approach Using C

Using One Pointer for Many Variables Computer Science: A Structured Programming Approach Using C

One Variable with Many Pointers Computer Science: A Structured Programming Approach Using C

Using A Variable with Many Pointers Computer Science: A Structured Programming Approach Using C

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

An Unworkable Exchange Computer Science: A Structured Programming Approach Using C

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

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

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

Pointers to Pointers Computer Science: A Structured Programming Approach Using C

Using Pointers to Pointers Computer Science: A Structured Programming Approach Using C

Using pointers to pointers Computer Science: A Structured Programming Approach Using C

Using pointers to pointers Computer Science: A Structured Programming Approach Using C

Using pointers to pointers Computer Science: A Structured Programming Approach Using C

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

Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

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

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

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

Invalid rvalue Expressions Computer Science: A Structured Programming Approach Using C

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

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

A Common Program Design Computer Science: A Structured Programming Approach Using C

Using Pointers as Parameters Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

Quadratic Roots Computer Science: A Structured Programming Approach Using C

12-4 Unions The union is a construct that allows memory to be shared by different types of data. This redefinition can be as simple as redeclaring an integer as four characters or as complex as redeclaring an entire structure. Computer Science: A Structured Programming Approach Using C

Unions Computer Science: A Structured Programming Approach Using C

Demonstrate Effect of Union Computer Science: A Structured Programming Approach Using C

Demonstrate Effect of Union Computer Science: A Structured Programming Approach Using C

A Name Union Computer Science: A Structured Programming Approach Using C

Demonstrate Unions in Structures Computer Science: A Structured Programming Approach Using C

Demonstrate Unions in Structures Computer Science: A Structured Programming Approach Using C

Demonstrate Unions in Structures Computer Science: A Structured Programming Approach Using C

Convert IP Address to long Computer Science: A Structured Programming Approach Using C

Convert IP Address to long Computer Science: A Structured Programming Approach Using C

Convert IP Address to long Computer Science: A Structured Programming Approach Using C