Pointer Data Type and Pointer Variables

Slides:



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

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.
Pointers. 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.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
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++ Programming: Basic Elements of C++.
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++ 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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
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.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers What is the data type of pointer variables?
What Actions Do We Have Part 1
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers and Pointer-Based Strings
Pointer Data Type and Pointer Variables II
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Variables with Memory Diagram
CS1201: Programming Language 2
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Pointer Data Type and Pointer Variables III
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Dynamic Memory A whole heap of fun….
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
Arithmetic Operations
What Actions Do We Have Part 1
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointer Data Type and Pointer Variables By:Nouf Aljaffan Edited by : Nouf Almunyif Pointer Data Type and Pointer Variables 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 data using pointers you will learn how to allocate and deallocate memory during program execution using pointers.

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

What is the Pointer Value Pointer variable A variable whose content is an address (that is, a memory address).

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. int * p; int* p; int *p;

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 operator 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 X p

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. num = 78; p = # *p = 24;

Example #include <iostream> #include <string> 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; }