What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Similar documents
Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES


C++ for Java Programmers

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Arrays, Pointers and Memory Management

DECLARAING AND INITIALIZING POINTERS

Homework #3 CS2255 Fall 2012

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Exam 3 Chapters 7 & 9

CS201- Introduction to Programming Current Quizzes

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Dynamic Allocation of Memory

[0569] p 0318 garbage

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointer Data Type and Pointer Variables

Pointers, Dynamic Data, and Reference Types

Introduction to Programming Using Java (98-388)

Assist. Prof. Dr. Caner ÖZCAN

C Pointers. 6th April 2017 Giulio Picierro

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

QUESTION BANK SUB: COMPUTER SCIENCE(083)

Pointers. Variable Declaration. Chapter 10

Pointers II. Class 31

Dynamic Allocation in C

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Pointers and References

CHAPTER 4 FUNCTIONS. 4.1 Introduction

a data type is Types

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Arrays and Pointers (part 1)

CS 161 Exam II Winter 2018 FORM 1

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

C++ for Java Programmers

Language comparison. C has pointers. Java has references. C++ has pointers and references

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Short Notes of CS201

CA31-1K DIS. Pointers. TA: You Lu

CS201 - Introduction to Programming Glossary By

CS242 COMPUTER PROGRAMMING

Computer Programming Unit 3

C Programming Basics II

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CSC 211 Intermediate Programming. Arrays & Pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Chapter 11: Pointers

Constants, References

CS201 Some Important Definitions

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Arrays and Pointers (part 1)

School of Science and Technology

CS201 Latest Solved MCQs

Pointers and Dynamic Memory Allocation

What is an algorithm?

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Dynamic Memory Allocation

CS2255 HOMEWORK #1 Fall 2012

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Dynamic Allocation in C

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Objectives. In this chapter, you will:

Computer Programming

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Lecture 3 Tao Wang 1

Pointers. Memory. void foo() { }//return

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Chapter 9. Pointers and Dynamic Arrays

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Algorithms & Data Structures

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Chapter-8 DATA TYPES. Introduction. Variable:

Lecture 2: C Programm

Object-Oriented Principles and Practice / C++

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

04-19 Discussion Notes

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Transcription:

CHAPTER 08 POINTERS

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++ s most useful and powerful features.

How Pointers are one of the C++ s most useful and powerful features? First : Pointer provide the means through which the memory location of variable can be directly accessed and hence it can be manipulated in the way as required.

How Pointers are one of the C++ s most useful and powerful features? Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines.

C++ MEMORY MAP STACK HEAP STACK - This area is used for function calls return address, argument and local variables HEAP This area is used for Dynamic Memory Allocation GLOBAL VARIABLES PROGRAM CODE GLOBAL VARIABLES - This area is used to store global variables where they exists as long as the program continues

DYNAMIC AND STATIC MEMORY ALLOCATION The Golden Rule of computer states that anything or everything that needs to be processed must be loaded into internal memory before its processing takes place. Therefore the main memory is allocated in two ways, STATIC MEMORY ALLOCATION DYNAMIC MEMORY ALLOCATION

STATIC MEMORY ALLOCATION In this technique the demanded memory is allocated in advance that is at the compilation time is called static memory allocation. For example, int s; The compiler allocates the 2 bytes of memory before its execution.

DYNAMIC MEMORY ALLOCATION In this technique the memory is allocated as and when required during run time (program execution) is called Dynamic memory allocation. C++ offers two types of operators called new and delete to allocate and de-allocate the memory at runtime.

FREE STORE FREE STORE is a pool of unallocated heap memory given to a program that is used by the program for dynamic allocation during execution.

DECLARATION AND INITIALIZATION OF POINTERS Pointer variables are declared like normal variables except for the addition of unary operator * (character) The General Format of Pointer variable declaration is, type * var_name; where, type refers to any C++ valid data type and var_name is any valid C++ variable

DECLARATION AND INITIALIZATION OF POINTERS For example, int *iptr; // integer pointer variable points to //another integer variable s location. float *fptr; // float type pointer variable points to //another float type variable s location. char *cptr; // character type pointer variable //points to another char type variable s location.

Two Special Operators Two special operators are used * and & are with pointers. & operator is called as address of operator which provides the address of operand. * Operator is called as at address and also called as differencing which provides the value being pointed by pointer.

Two Special Operators For example, int i = 25; int *iptr; iptr= &i; i iptr 25 1050 1050

NULL Pointer (ZERO POINTER) A pointer variable must not remain uninitilized since uninitialized pointer cause system crash. Even if you do not have legal pointer value to initialize a pointer, you can initialize it with NULL pointer (ZERO POINTER). int *iptr=null; Note: In C++ version 11 nullptr keyword is introduced to assign null. nullptr is legal empty null pointer.

POINTER AIRTHMETIC Only two arithmetic operators, addition and subtraction may be performed on pointers. When you add 1 to a pointer, you are actually adding the size of what ever the pointer pointing at. For example, iptr++; iptr--;

POINTER AIRTHMETIC Only two arithmetic operators, addition and subtraction may be performed on pointers. When you add 1 to a pointer, you are actually adding the size of what ever the pointer pointing at. For example, iptr++; iptr--; Note: In pointer arithmetic all pointers increase and decrease by the length of the data type they point to.

DYNAMIC ALLOCATION OPERATORS - new OPERATOR C++ offers two types of operators called new and delete to allocate and de-allocate the memory at runtime. Since these two operators operate upon free store memory, they are also called as free store operators Syntax : pointer_variable = new data type; where pointer_variable pointer variable and datatype is valid C++ datatype and new is operator which allocates the memory (size of data type) at run time.

DYNAMIC ALLOCATION OPERATORS - new OPERATOR For example, int *iptr=new int; char *cptr=new char; float *fptr=new float; Once a pointer points to newly allocated memory, data values can be stored there using at address operator *cptr= a ; *fptr=1.34; *iptr=89;

DYNAMIC ALLOCATION OPERATORS - new OPERATOR Initializing values at the time of declaration one can rewrite like, int *iptr=new int(89); char *cptr=new char( a ); float *fptr=new float(1.34);

DYNAMIC ALLOCATION OPERATORS CREATING DYNMIC ARRAYS 1D ARRAYS: Syntax : pointer_variable = new data type[size]; int *value=new int[10]; It will create memory space from the free store to store 10 integers. Initilization while declaring dynamic array is not possible but it is included in C++ compiler ver 11.

DYNAMIC ALLOCATION OPERATORS - new OPERATOR CREATING DYNMIC ARRAYS (2D ARRAYS): One needs to be tricky while defining 2D array as, int *val,r,c,i,j; cout<< Enter dimensions ; cin>>r>>c; val=new int [ r * c ] ; To read the elements of the 2D array for(i=0;i<r;i++) { for(j=0;j<c;j++) cin>> val [ i *c + j ] ; }

The life time of the variable or object created by new is not restricted to the scope in which it is created. It lives in the memory until explicitly deleted using the delete operator. Syntax: DYNAMIC ALLOCATION OPERATORS - delete OPERATOR delete pointer_variable; For example, delete iptr; FOR ARRAYS : Syntax : delete [ ] arraypointer_variable; delete [ ] val;

DYNAMIC ALLOCATION OPERATORS MEMORY LEAK Improper use of new and delete may lead to memory leaks. Make sure that the memory allocated through new must be deleted through delete. Otherwise this may lead to adverse effect on the system

It is faster to use an element pointer than an index when scanning arrays, For 1D Array, S[0]= *( S + 0 ) or *S or *(S) S[1] = *( S + 1 ) S[2] = *( S + 2 ) FOR 2D Arrays, ARRAYS S [ 0 ] [ 0 ] = *( S [ 0 ] + 0 ) or * ( * ( S + 0 ) ) S [ 0 ] [ 1 ] = *( S [ 0 ] + 1 ) or * ( * ( S + 1 ) ) S [ 1 ] [ 2 ] = *( S [ 1 ] + 2 ) or * ( * ( S + 1 ) + 2 )

POINTERS AND STRINGS A string is a one dimensional array of characters terminated by a null \0. You have learnt in 11 std a string can be defined and processed as, char name [ ] = POINTER ; for ( I = 0 ; name [ i ]! = \ 0 ; i + + ) cout<<name[i]; Alternatively the same can be achieved by writing, char name [ ] = POINTER ; char *cp; for ( cp = name ; * cp! = \ 0 ; cp + + ) cout<< *cp ;

Another Example, POINTERS AND STRINGS char *names [ ] = { Sachin, Kapil, Ajay, Sunil, Anil }; char *t; t=name[1] ; //pointing to string Kapil cout<<*t; // will produce out put Kapil Similarly T=name[3]; will point to?

POINTERS AND CONST Constant pointer mean that the pointer in consideration will always point to the same address. Its address ( to which it is pointing to ) can not be modified. For example, int n=44; int *const ptr = &n; ++(*ptr); // allowed since it modifies the content. ++ptr; // illegal because pointer ptr is constant pointer

CBSE QUESTION PAPER QNO 1 (d) 3 Marks QNO 1 (e) 3 Marks

1(d) What will be the output of the following program : 3 Delhi 2004 #include<iostream.h> #include<ctype.h> #include<conio.h> #include<string.h> void ChangeString(char Text[], int &Counter) { char *Ptr = Text; int Length = strlen (Text); for ( ;Counter<Length-2; Counter+=2, Ptr++) { * (Ptr + Counter) = toupper( * (Ptr + Counter) ); } } void main() { clrscr(); int Position = 0; char Messaget[] = Pointers Fun ; ChangeString (Message, Position); cout<<message<< @ <<Position; }