Pointers.

Slides:



Advertisements
Similar presentations
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Advertisements

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Topics discussed in this section:
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
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.
Topic 8 – Introduction to Pointers and Function Output Parameters.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
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
Computer Science 210 Computer Organization Pointers.
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Topics discussed in this section:
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Copyright © – Curt Hill Pointers A Light Introduction.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Computer Science 210 Computer Organization
Introduction to Programming Using C
Pointers Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
Functions Review.
Pointers and Pass By Reference
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
Pointers.
Chapter 9 Pointers Objectives
Lecture 6 C++ Programming
Pointers Psst… over there.
Pointers and References
Using local variable without initialization is an error.
Computer Science 210 Computer Organization
Pointers Psst… over there.
Pointer Basics Psst… over there.
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:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 8 Arrays Objectives
Pointers & Functions.
Topics discussed in this section:
Topics discussed in this section:
Introduction to C++ Programming Language
Topics discussed in this section:
Pointers Lecture 2 Tue, Jan 24, 2006.
Pointers Lecture 1 Thu, Jan 15, 2004.
C++ Pointers and Strings
Pointers Pointers point to memory locations
Chapter 8 Arrays Objectives
Exam 2 Exam 2 Regrading Average: 69 TA: Fardad
Pointers and Pointer-Based Strings
Chapter 9: Pointers and String
Pointers & Functions.
Pointer Basics Psst… over there.
Topics discussed in this section:
Pointers and References
C Pointers Systems Programming.
C++ Pointers and Strings
Pointers and pointer applications
Presentation transcript:

Pointers

Topics discussed in this section: Pointers for Inter-function Communication One of the most useful applications of pointers is in functions. 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

FIGURE -1 An Unworkable Exchange

FIGURE -2 Exchange Using Pointers

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.

FIGURE -3 Functions Returning Pointers

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.

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.

FIGURE -4 Pointers to Pointers

FIGURE -5 Using Pointers to Pointers

Using pointers to pointers PROGRAM Using pointers to pointers

Using pointers to pointers PROGRAM Using pointers to pointers

Using pointers to pointers PROGRAM Using pointers to pointers

Topics discussed in this section: 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

Demonstrate Size of Pointers PROGRAM Demonstrate Size of Pointers

Demonstrate Size of Pointers PROGRAM Demonstrate Size of Pointers

Demonstrate Size of Pointers PROGRAM Demonstrate Size of Pointers

FIGURE 6 Dereference Type Compatibility

A void pointer cannot be dereferenced. Note A void pointer cannot be dereferenced.

FIGURE 7 Dereference Level Compatibility

FIGURE 8 A Common Program Design

FIGURE -9 Using Pointers as Parameters

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots

PROGRAM Quadratic Roots