Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Data Structures Using C++ 2E
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
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++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
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. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review 1 List Data Structure List operations List Implementation Array Linked List.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Computer Skills2 for Scientific Colleges
Motivation and Overview
Pointers Psst… over there.
Programming fundamentals 2 Chapter 2:Pointer
Pointers Psst… over there.
Pointer Basics Psst… over there.
Pointers.
Pointers and Dynamic Variables
Dynamic Memory.
Dynamic Memory Copy Challenge
Pointer Basics Psst… over there.
Presentation transcript:

Pointers

Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Pointer:  A variable that can only store the address of a memory location (usually the address of other variable). 300 x 500 a 300 pointerpointee

Syntax:  type * identifier; i.e. int *p; G p 258 pointer

 int a=50;  int * p;  p = &a; p 500 pointer 50 a 300 pointee p points to a 300

* and & operators  Dereferencing *p value at where pointer p is pointing  Address of &a address of variable a

Example: void main(){ int *p; int a=50; p = &a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; }

Example: void main(){ =>int *p; int a=50; p = &a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; } p 500 pointer

Example: void main(){ int *p; =>int a=50; p = &a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; } p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; =>p = &a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; } 300 p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; p = &a; =>cout<<a<<endl;// 50 cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; } 300 p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; p = &a; cout<<a<<endl;// 50 =>cout<<&a<<endl;// 300 cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; } 300 p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; p = &a; cout<<a<<endl;// 50 cout<<&a<<endl;// 300 =>cout<<p<<endl;// 300 cout<<*p<<endl; cout<<&p<<endl; } 300 p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; p = &a; cout<<a<<endl;// 50 cout<<&a<<endl;// 300 cout<<p<<endl;// 300 =>cout<<*p<<endl;// 50 cout<<&p<<endl; } 300 p 500 pointer 50 a 300 pointee

Example: void main(){ int *p; int a=50; p = &a; cout<<a<<endl;// 50 cout<<&a<<endl;// 300 cout<<p<<endl;// 300 cout<<*p<<endl;// 50 =>cout<<&p<<endl;// 500 } 300 p 500 pointer 50 a 300 pointee

Example 2: void main(){ int *p; int a=50; cout<<*p<<endl; } What would be the output? Runtime error!!! p 500 pointer 50 a 300 pointee Crash!!!

Dangling Pointer int *p; Note: Dereferencing a dangling pointer is a serious runtime error. G p 500 pointer Dangling pointer

Problem: We can’t differentiate that whether a pointer is dangling or has a valid address. What’s the solution?

NULL Pointer int *p; p = NULL;// points to // nothing Tip:Always initialize a pointer with NULL if you don’t have a valid address. This can save a lot of your time on debugging. p 500 pointer

Example: void main(){ int *p = NULL; int a=50; p = &a; cout<<a<<endl; cout<<&a<<endl; cout<<p<<endl; cout<<*p<<endl; cout<<&p<<endl; }

Pointer Assignment  An assignment operation b/w two pointers makes them points to the same pointee.

 int a=50;  int * p;  p = &a;  int p2 = p; p 500 pointer 50 a 300 pointee p points to a 300 p2 200 pointer 300 Note: Pointer assignment only copies the address and not the memory they are pointing to.

Shallow Copy 300 p 500 pointer 50 a 300 pointee p points to a 300 p2 200 pointer

Problem with Shallow Copy 300 p 500 pointer 50 a 300 pointee p points to a 300 p2 200 pointer What if p delete a?

What is the solution to the problem?

Deep Copy 300 p 500 pointer 50 a 300 pointee p points to a 100 p2 200 pointer 50 a2 100 pointee

Pointer type and Arithmetic  What is type of pointer? int * p; A pointer has not type!!! It’s the type of variable the pointer p will point to.

 Why we need to specify the type of variable a pointer points to?

1. int a=10; int *p = &a; *p = 4; // how many bytes of copy? It would be 1 byte in case a was a char.

2. Pointer & Arrays int arr[ ]={5,6,7,8,4,3,5}; // size of arr? int *p = arr; // why & isn’t use before arr? cout<<arr<<endl;// 40 cout<<p<<endl;// 40 p=p+3; p 500 pointer

2. Pointer & Arrays int arr[ ]={5,6,7,8,4,3,5}; // size of arr? int *p = arr; // why & isn’t use before arr? cout<<arr<<endl;// 40 cout<<p<<endl;// 40 p=p+3; cout<<p<<endl;// 46 cout<<*p<<endl;// p 500 pointer

Example 3: void main( ){ => int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl; cout<<*arr<<endl; cout<<*(arr+4)<<endl; cout<<p<<endl; cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; }

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; => int *p = arr; cout<< arr<<endl; cout<<*arr<<endl; cout<<*(arr+4)<<endl; cout<<p<<endl; cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; => cout<< arr<<endl;// 0x40 cout<<*arr<<endl; cout<<*(arr+4)<<endl; cout<<p<<endl; cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 => cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl; cout<<p<<endl; cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 => cout<<*(arr+4)<<endl;// 4 cout<<p<<endl; cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 => cout<<p<<endl;//0x40 cout<<&p<<endl; cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 cout<<p<<endl;//0x40 => cout<<&p<<endl;//0x500 cout<<*p<<endl; cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 cout<<p<<endl;//0x40 cout<<&p<<endl;//0x500 => cout<<*p<<endl;// 5 cout<<p[3]<<endl; cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 cout<<p<<endl;//0x40 cout<<&p<<endl;//0x500 cout<<*p<<endl;// 5 => cout<<p[3]<<endl;// 8 cout<<p++<<endl; cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 cout<<p<<endl;//0x40 cout<<&p<<endl;//0x500 cout<<*p<<endl;// 5 cout<<p[3]<<endl;// 8 => cout<<p++<<endl;// 0x40 cout<<++*p<<endl; } p 500 pointer

Example 3: void main( ){ int arr[ ]={5,6,7,8,4,3,5}; int *p = arr; cout<< arr<<endl;// 0x40 cout<<*arr<<endl;// 5 cout<<*(arr+4)<<endl;// 4 cout<<p<<endl;//0x40 cout<<&p<<endl;//0x500 cout<<*p<<endl;// 5 cout<<p[3]<<endl;// 8 cout<<p++<<endl;// 0x40 => cout<<++*p<<endl;// 7 } p 500 pointer

Arrays of Pointer data_type *arr[size]; i.e. int *a[7]; char *c[7]; ******* *******

Uses:  Sharing & Cost saving  Pass-by-ref  Dynamic Memory Allocation  Making Complex Data Structures  Linked list, Tree etc.

1. Sharing & cost saving void main(){ int a=5,b=8; swap(&a,&b); } void swap(int *a,int *b){ int t = *a; *a = *b; *b = t; }

2. DMA void main(){ int size=0; int *p=NULL; cout<<“Enter No. of Students”<<endl; cin>>size; p = (int *)malloc(size*sizeof(int)); for(int i=0;i<size;i++){ cout<<“Enter St “<<i<<“ Marks”<<endl; cin>>p[i]; //= cin>>*p; p++; }

3. Data Structures Data Pointer Data Pointer Data Pointer Data Pointer

References:  Pointer And Memory (  Pointer Basics (  c/lesson6.html c/lesson6.html  tml