Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
True or false A variable of type char can hold the value 301. ( F )
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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 A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 13: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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.
Pointers *, &, array similarities, functions, sizeof.
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.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Pointers and Pointer-Based Strings
Pointer Data Type and Pointer Variables II
Pointers and References
Pointer Data Type and Pointer Variables
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Presentation transcript:

Pointer Data Type and Pointer Variables

Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer Variables Operations on Pointer Variables Functions and Pointers Classes, Structs, and Pointer Variables

Pointer Data Type and Pointer Variables  C++’s data types are classified into three categories:  Simple  Structured  Pointers.

What is the Pointer Value

Pointer Data Type and Pointer Variables  There is no name associated with pointer data types  If no name is associated with a pointer data type, how do you declare pointer variables?  p is called a pointer variable of type int  ch is called a pointer variable of type char.

Pointer Declaration *  Thus, the character * can appear anywhere between the data type name and the variable name.

Pointer Declaration  int* p, q;  In this statement:  only p is the pointer variable, not q.  Here, q is an int variable.  we prefer to attach the character * to the variable name. So the preceding statement is written as: int *p, q;

Pointer Operators  C++ provides two operators operator to work with pointers.  (&) the address of operator  (*) the dereferencing

Address of Operator (&)  is a unary operator that returns the address of its operand.  For example, given the statements: int x; int *p;  The statement: p = &x;  assigns the address of x to p. That is, x and the value of p refer to the same memory location

Dereferencing Operator (*)  referred to as indirection operator  refers to the object to which its operand (that is, the pointer) points.  For example, given the statements: 25 Xp

Dereferencing Operator (*)  Let us consider the following statements: In these statements:  p is a pointer variable of type int  num is a variable of type int.  Let us assume that memory location 1200 is allocated for p, and memory location 1800 is allocated for num.

Dereferencing Operator (*)  Consider the following statements. 1. num = 78; 2. p = # 3. *p = 24;

Example #include using namespace std; //=============================== int main() { int *p; int x = 37; cout <<"x= " << x<<endl; p=&x; cout <<"*p= "<< *p <<", x= "<<x<<endl; *p=85; cout <<"*P= " <<*p <<", x= "<<x<<endl; cout <<" Address of p is : "<< &p <<endl; cout<<"value of p : "<< p<<endl; cout<< " value of memory location pointed to by *p = "<<*p<<endl; cout<<"Address of X = " << &x<<endl; cout <<" Value of x = " << x<<endl; return 0; }

Summary:

Initializing Pointer Variables  C++ does not automatically initialize variables  pointer variables must be initialized if you do not want them to point to anything.  Pointer variables are initialized using the following two statements :  p = NULL;  p = 0 ;  The number 0 is the only number that can be directly assigned to a pointer variable.

Operations on Pointer Variables  The value of one pointer variable can be assigned to another pointer variable of the same type.  Two pointer variables of the same type can be compared for equality, and so on.  Integer values can be added and subtracted from a pointer variable.  The value of one pointer variable can be subtracted from another pointer variable.

Operations on Pointer Variables  copies the value of q into p. After this statement executes, both p and q point to the same memory location.  Any changes made to *p automatically change the value of *q, and vice versa.

Comparison The expression:  p == q evaluates to true if p and q have the same value—that is, if they point to the same memory location. Similarly, the expression:  p != q evaluates to true if p and q point to different memory locations.

Decrement and increment  ++ increments the value of a pointer variable by the size of the memory to which it is pointing.  Similarly, -- the value of a pointer variable by the size of the memory to which it is pointing.  to explain the increment and decrement operations on pointer variables: Recall that the size of the memory allocated for an  int variable is 4 bytes  a double variable is 8 bytes  a char variable is 1 byte.  studentType is 40 bytes.

Decrement and increment The statement:  p++; or p = p + 1; increments the value of p by 4 bytes because p is a pointer of type int. Similarly, the statements:  q++;  chPtr++; increment the value of q by 8 bytes and the value of chPtr by 1 byte, respectively. The statement:  stdPtr++; increments the value of stdPtr by 40 bytes.

Cont.  Moreover, the statement:  p = p + 2;  increments the value of p by 8 bytes.  Thus, when an integer is added to a pointer variable, the value of the pointer variable is incremented by the integer times the size of the memory that the pointer is pointing to.  Similarly, when an integer is subtracted from a pointer variable, the value of the pointer variable is decremented by the integer times the size of the memory to which the pointer is pointing.

Notes  Pointer arithmetic can be very dangerous.  The program can accidentally access the memory locations of other variables and change their content without warning. leaving the programmer trying to find out what went wrong.

Functions and Pointers  A pointer variable can be passed as a parameter to a function either by value or by reference.  In the function pointerParameters, both p and q are pointers. The parameter p is a reference parameter; the parameter q is a value parameter.  Furthermore, the function pointerParameters can change the value of *q, but not the value of q. However, the function pointerParameters can change the value of both p and *p.

Example #include using namespace std; void f(int *j); int main() { int i =0 ; int *p; p = &i; // p now points to i cout<<"*p is " << *p<<endl << "i= " << i <<endl; f(p); cout << "i is " << i << endl; return 0; } void f(int *j) { *j = 100; // var pointed to by j is assigned 100 cout <<"*j is : "<< *j<<endl; }

Functions and Pointers  Passing a pointer as an argument to a function is in some ways similar to passing a reference.  They both permit the variable in the calling program to be modified by the function. However,  the mechanism is different. A reference is an alias for the original variable, while a pointer is  the address of the variable.

Pointers and Function Return Values  In C++, the return type of a function can be a pointer. For example, the return type of the function:

Example #include using namespace std; double * GetSalary() { double salary = 26.48; double *HourlySalary = &salary; return HourlySalary; } int main() { double hours = 46.50; double salary = *GetSalary(); cout << "Weekly Hours: " << hours << endl; cout << "Hourly Salary: " << salary << endl; double WeeklySalary = hours * salary; cout << "Weekly Salary: " << WeeklySalary << endl; return 0; }

Classes, Structs, and Pointer Variables by default, all members of a class are private. by default, all members of a struct are public. private public

Member access operator (.)  The following statement stores 3.9 in the component GPA of the object student: *studentPtr.gpa = 3.9;  Consider the expression *studentPtr.gpa.  Because. (dot) has a higher precedence than *, the expression studentPtr.gpa evaluates first.  The expression studentPtr.gpa would result in a syntax error, as studentPtr is not a struct variable, so it has no such component as GPA.

Member access operator (->)  C++ provides another operator called the member access operator arrow, ->.

Example #include using namespace std; class classexample{ private : int x; public : void setX(int a ) {x=a;} void print(){cout<<"x= " <<x<<endl;} }; void main (){ classexample ob; classexample *obpointer; obpointer = &ob; obpointer->setX(5); obpointer->print(); } X ob obpointer X ob 5