© 2016 Pearson Education, Ltd. All rights reserved.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
 2006 Pearson Education, Inc. All rights reserved Pointers.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Pointers C++ How to Program, 9/e
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture 17: The Last Puzzle Piece with Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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 Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
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.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers Introduction CSCI 230
Pointers Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
FIGURE 9-5 Integer Constants and Variables
8 Pointers.
Pointer Data Type and Pointer Variables
C Structures, Unions, Bit Manipulations and Enumerations
Topics discussed in this section:
Systems Programming Concepts
Pointers and Pointer-Based Strings
5.1 Introduction Pointers Powerful, but difficult to master
C++ Programming Lecture 17 Pointers – Part I
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
C Pointers Systems Programming.
Chapter 2, Part III Arithmetic Operators and Decision Making
C Pointers Another ref:
Dynamic Data Structures
Presentation transcript:

© 2016 Pearson Education, Ltd. All rights reserved. Chapter 7 C Pointers C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 7.1  Introduction In this chapter, we discuss one of the most powerful features of the C programming language, the pointer. Pointers enable programs to simulate pass-by-reference, to pass functions between functions, and to create and manipulate dynamic data structures, i.e., data structures that can grow and shrink at execution time, such as linked lists, queues, stacks and trees. © 2016 Pearson Education, Ltd. All rights reserved.

7.2 Pointer Variable Definitions and Initialization Pointers are variables whose values are memory addresses. Normally, a variable directly contains a specific value. A pointer, on the other hand, contains an address of a variable that contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value (Fig. 7.1). Referencing a value through a pointer is called indirection. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

7.2 Pointer Variable Definitions and Initialization (cont.) Declaring Pointers Pointers, like all variables, must be defined before they can be used. The definition int *countPtr; specifies that variable countPtr is of type int * (i.e., a pointer to an integer) and is read (right to left), “countPtr is a pointer to int” or “countPtr points to an object of type int.” © 2016 Pearson Education, Ltd. All rights reserved.

7.2 Pointer Variable Definitions and Initialization (cont.) The * applies only to countPtr in the definition. When * is used in this manner in a definition, it indicates that the variable being defined is a pointer. Pointers can be defined to point to objects of any type. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 7.3  Pointer Operators The &, or address operator, is a unary operator that returns the address of its operand. For example, assuming the definitions int y = 5; int *yPtr; the statement yPtr = &y; assigns the address of the variable y to pointer variable yPtr. Variable yPtr is then said to “point to” y. Figure 7.2 shows a schematic representation of memory after the preceding assignment is executed. © 2016 Pearson Education, Ltd. All rights reserved.

7.3 Pointer Operators (Cont.) The Indirection (*) Operator The * operator, commonly referred to as the indirection operator or dereferencing operator, returns the value of the object to which its operand (i.e., a pointer) points. For example, the statement printf("%d", *yPtr); prints the value of variable y, namely 5. Using * in this manner is called dereferencing a pointer. © 2016 Pearson Education, Ltd. All rights reserved.

7.3 Pointer Operators (Cont.) Demonstrating the & and * Operators Figure 7.4 demonstrates the pointer operators & and *. The printf conversion specifier %p outputs the memory location as a hexadecimal integer on most platforms. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.   Null Pointers It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. © 2016 Pearson Education, Ltd. All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved.   Null Pointer example © 2016 Pearson Education, Ltd. All rights reserved.