Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
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.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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 Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
 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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Chapter 8 Arrays, Strings and Pointers
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Student Book An Introduction
CNG 140 C Programming (Lecture set 10)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 10: Pointers 1.
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
Chapter 9: Pointers and String
CISC181 Introduction to Computer Science Dr
Introduction to Pointers
Presentation transcript:

Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address of a variable that contains a specific value. We use pointers extensively to create and manipulate dynamic data structures ( linked lists, queues, stacks, and trees), and to simulate call by reference.

Pointer Pointers should be initialized either when they are declared or in an assignment statement. Pointer can be initialized to 0, NULL, or an address. A pointer with the value NULL points to nothing. Initializing a pointer to 0 is equivalent to initializing to NULL (NULL is preferred); NULL is a symbolic constant defined in header file.

Representation of a Pointer int x = 84; // declaration int *ptr; // declaration ptr = &x; 84 ptr x 84 x X directly references a variable whose value is 84 ptr indirectly references a variable whose value is 84 Assigned the address of the variable x to pointer variable ptr. Direct value of ptr is the address of x.

Representation of x and ptr in memory int x = 84; // declaration int *ptr; // declaration ptr = &x; // assigned the address of the // variable x to pointer variable ptr ptr Ptr indirectly references a variable whose value is x X directly references a variable whose value is

Example #include void determine_sign(double num, char *signp); int main(void) { double value; char sn; cout ”; cin >>value; determine_sign( value, &sn); cout << “The sign of “ << value << “is “ << sn <<endl; return (0); }

Example void determine_sign(double num, char *signp) { if (num < 0) *signp = ‘-’; else if (num == 0) *signp = ‘ ‘; else *signp = ‘+’; }

Different ways to pass arguments to a function Two ways to invoke functions: –Call by value –Call by reference Call by value: When arguments are passed call by value, –a copy of the argument’s value is made and passed to the called function. –Changes to the copy do not affect an original variable’s value in the caller.

Call by Reference When an argument is passed by reference, the caller actually allows the called function to modify the original variable’s value. It is possible to simulate call by reference by using address operators and indirection operators.

Cube a variable using call by value #include int cubeByValue(int); int main() { int number1, number = 5; cout << “The original value of number is “<< number <<endl; number1 = cubeByValue(number); cout << “The new value of number is “<< number << endl; cout << “The value of number1 is “<< number1 << endl; return 0; } int cubeByValue(int n) { return n * n * n; }

Cube a variable using call by reference #include void cubeByReference(int *); int main() { int number = 5; cout << “The original value of number is “<< number <<endl; cubeByReference(&number); cout << “The new value of number is “<< number << endl; return 0; } void cubeBy Reference(int *nPtr) { *nPtr = *nPtr ** nPtr * *nPtr; }

Pointer Arithmetic A limited set of arithmetic operators may be performed on pointers. A pointer may be incremented (++) or decremented (--). An integer may be added to a pointer (+ or +=). An integer may be subtracted from a pointer (+ or +=). One pointer may be subtracted from another.

Example int a[5]= {1,2,3,4,5}; int *ptr; ptr = a; When an integer is added to or subtracted from a pointer, the pointer is not simply incremented or decremented by that integer. It depends on the size of the object a pointer point to => machine dependent a + 1a + 3 ptr a

Example (Increment) When an integer is added to a pointer, the pointer is incremented by that integer times the size of the object to which the pointer points to. int *ptr; ptr = a; ptr += 2; will produce: 3008 ( * 4) integer is stored in 4 bytes of memory a + 1 a + 3 ptr a

Example (Decrement) When an integer is subtracted from a pointer, decremented by that integer times the size of the object to which the pointer points to. int *ptr; ptr = a; ptr += 2; ptr -= 1; will produce: 3004 ( * 4) integer is stored in 4 bytes of memory a + 1 a + 3 ptr a

Subtraction Pointer variables may be subtracted from one another. ptr1 contains the location ptr2 contains the location x = ptr2 - ptr1; = 2 will assign to x the number of array elements from ptr1 to ptr ptr2ptr1 a

Relationship between Pointers and Arrays Arrays and Pointers are intimately related in C. Array subscripting notation is converted to pointer notation during compilation. Array name is a pointer to the first element of the array. ptr = &a[0] *(ptr + 3)  a[3] &a[3]  (ptr + 3) a += 2 invalid => attempts to modify the value of array name with pointer arithmetic a[1]a[3] ptr a Offset to the pointer

Array of Pointers Array may contain pointers (e.g. string array). In C, string is essentially a pointer to its first character. Each entry in an array of strings is actually a pointer to the first character of a string. Example: char *names[4] = {“John”, “Cynthia”, “David”, “Daniel”}; names[4] => an array of 4 elements. char * => each element of array names is of type “pointer to char.” Four array elements are: “John”, “Cynthia”, “David”, “Ruben”. Each of these are stored in memory as a NULL-terminated character string that is one character longer than the number of characters between quotes. E.g. Four strings are 5, 8, 6, and 7.

Example of array of pointers It seems that the strings are placed in the names array, Actually, only the pointers are stored in the array. Each pointer points to the first character of its corresponding string. Names is a fixed size array. However it provides access to character strings of any length. Names could have been placed into a double array of fixed number of columns per row => memory could be wasted. ‘J’‘\0’ ‘D’ ‘h’ ‘C’ ‘n’ ‘y’‘n’ ‘o’ ‘\0’‘a’‘i’‘h’ ‘n’ ‘i’‘v’‘a’ ‘t’ ‘l’‘a’‘D’ ‘\0’‘d’ ‘e’ ‘i’ ‘\0’ names[0] names[1] names[2] names[3]

Pointer to Functions Pointer to a function contains the address of the function in memory. A function name is really the starting address in memory of code that performs the function’s task. Pointers to functions can be passed to functions, returned from functions, stored in arrays, and assigned to another function pointers. int (*compare)(int num[1], int num[4]) Parenthesis s are needed around *compare because * has lower precedence than parentheses enclosing the function parameters.