SlideShare a Scribd company logo
www.cppforschool.com
Pointer
Accessing address of a variable
Computer‟s memory is organized as a linear collection of bytes. Every byte in
the computer‟s memory has an address. Each variable in program is stored at a
unique address. We can use address operator & to get address of a variable:
int num = 23;
cout << &num; // prints address in hexadecimal
POINTER
A pointer is a variable that holds a memory address, usually the location of
another variable in memory.
Defining a Pointer Variable
int *iptr;
iptr can hold the address of an int
Pointer Variables Assignment:
int num = 25;
int *iptr;
iptr = &num;
Memory layout
To access num using iptr and indirection operator *
cout << iptr; // prints 0x4a00
cout << *itptr; // prints 25
Similary, following declaration shows:
char *cptr;
float *fptr;
cptr is a pointer to character and fptr is a pointer to float value.
Pointer Arithmetic
Some arithmetic operators can be used with pointers:
- Increment and decrement operators ++, --
- Integers can be added to or subtracted from
pointers using the operators +, -, +=, and -=
Each time a pointer is incremented by 1, it points to the memory location of the
next element of its base type.
If “p” is a character pointer then “p++” will increment “p” by 1 byte.
If “p” were an integer pointer its value on “p++” would be incremented by 4
bytes.
Pointers and Arrays
Array name is base address of array
int vals[] = {4, 7, 11};
cout << vals; // displays 0x4a00
cout << vals[0]; // displays 4
Lets takes an example:
int arr[]={4,7,11};
int *ptr = arr;
What is ptr + 1?
It means (address in ptr) + (1 * size of an int)
cout << *(ptr+1); // displays 7
cout << *(ptr+2); // displays 11
Array Access
Array notation arr[i] is equivalent to the pointer notation *(arr + i)
Assume the variable definitions
int arr[]={4,7,11};
int *ptr = arr;
Examples of use of ++ and --
ptr++; // points at 7
ptr--; // now points at 4
Character Pointers and Strings
Initialize to a character string.
char* a = “Hello”;
a is pointer to the memory location where „H‟ is stored. Here “a” can be viewed
as a character array of size 6, the only difference being that a can be reassigned
another memory location.
char* a = “Hello”;
a gives address of „H‟
*a gives „H‟
a[0] gives „H‟
a++ gives address of „e‟
*a++ gives „e‟
Pointers as Function Parameters
A pointer can be a parameter. It works like a reference parameter to allow
change to argument from within function
#include<iostream>
using namespace std;
void swap(int *, int *);
int main()
{
int a=10,b=20;
swap(&a, &b);
cout<<a<<" "<<b;
return 0;
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
output:
20 10
Pointers to Constants and Constant Pointers
Pointer to a constant: cannot change the value that is pointed at
Constant pointer: address in pointer cannot change once pointer is initialized
Pointers to Structures
We can create pointers to structure variables
struct Student {int rollno; float fees;};
Student stu1;
Student *stuPtr = &stu1;
(*stuPtr).rollno= 104;
-or-
Use the form ptr->member:
stuPtr->rollno = 104;
Static allocation of memory
In the static memory allocation, the amount of memory to be allocated is
predicted and preknown. This memory is allocated during the compilation itself.
All the declared variables declared normally, are allocated memory statically.
Dynamic allocation of memory
In the dynamic memory allocation, the amount of memory to be allocated is not
known. This memory is allocated during run-time as and when required. The
memory is dynamically allocated using new operator.
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.
Dynamic Memory Allocation
We can allocate storage for a variable while program is running by using new
operator
To allocate memory of type integer
int *iptr=new int;
To allocate array
double *dptr = new double[25];
To allocate dynamic structure variables or objects
Student sptr = new Student; //Student is tag name of structure
Releasing Dynamic Memory
Use delete to free dynamic memory
delete iptr;
To free dynamic array memory
delete [] dptr;
To free dynamic structure
delete Student;
Memory Leak
If the objects, that are allocated memory dynamically, are not deleted using
delete, the memory block remains occupied even at the end of the program.
Such memory blocks are known as orphaned memory blocks. These orphaned
memory blocks when increase in number, bring adverse effect on the system.
This situation is called memory leak
Self Referential Structure
The self referential structures are structures that include an element that is a
pointer to another structure of the same type.
struct node
{
int data;
node* next;
}

More Related Content

What's hot (20)

ODP
Pointers in c++ by minal
minal kumar soni
 
PDF
C Pointers
omukhtar
 
PDF
Pointers & References in C++
Ilio Catallo
 
PPT
C pointers
Aravind Mohan
 
PDF
See through C
Tushar B Kute
 
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
PDF
Lecturer23 pointersin c.ppt
eShikshak
 
PPT
Lecture 8
Mohammed Saleh
 
PPT
Introduction to pointers and memory management in C
Uri Dekel
 
PPTX
Used of Pointer in C++ Programming
Abdullah Jan
 
PDF
Pointers_c
ahmed safwat
 
PPT
Pointers in c
Mohd Arif
 
PPTX
Pointers in C
Kamal Acharya
 
PPTX
C programming - Pointers
Wingston
 
PPTX
Pointers in C Language
madan reddy
 
PPTX
Pointers in c++
Vineeta Garg
 
PPT
C++ Pointers And References
verisan
 
PPT
Pointers
Nivetha Palanisamy
 
PPTX
Pointer in c
lavanya marichamy
 
PPT
Pointers in C
Prabhu Govind
 
Pointers in c++ by minal
minal kumar soni
 
C Pointers
omukhtar
 
Pointers & References in C++
Ilio Catallo
 
C pointers
Aravind Mohan
 
See through C
Tushar B Kute
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
Lecturer23 pointersin c.ppt
eShikshak
 
Lecture 8
Mohammed Saleh
 
Introduction to pointers and memory management in C
Uri Dekel
 
Used of Pointer in C++ Programming
Abdullah Jan
 
Pointers_c
ahmed safwat
 
Pointers in c
Mohd Arif
 
Pointers in C
Kamal Acharya
 
C programming - Pointers
Wingston
 
Pointers in C Language
madan reddy
 
Pointers in c++
Vineeta Garg
 
C++ Pointers And References
verisan
 
Pointer in c
lavanya marichamy
 
Pointers in C
Prabhu Govind
 

Viewers also liked (14)

PDF
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
PDF
Chpater29 operation-on-file
Deepak Singh
 
PDF
Chapter17 oop
Deepak Singh
 
PDF
Chapter22 static-class-member-example
Deepak Singh
 
PDF
Chapter24 operator-overloading
Deepak Singh
 
PDF
Chapter15 structure
Deepak Singh
 
PDF
Chapter26 inheritance-ii
Deepak Singh
 
PDF
Chapter19 constructor-and-destructor
Deepak Singh
 
PDF
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
PDF
Chapter20 class-example-program
Deepak Singh
 
PDF
Chapter25 inheritance-i
Deepak Singh
 
PDF
Chapter23 friend-function-friend-class
Deepak Singh
 
PDF
Revision notes for exam 2011 computer science with C++
Deepak Singh
 
PDF
+2 Computer Science - Volume II Notes
Andrew Raj
 
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
Chpater29 operation-on-file
Deepak Singh
 
Chapter17 oop
Deepak Singh
 
Chapter22 static-class-member-example
Deepak Singh
 
Chapter24 operator-overloading
Deepak Singh
 
Chapter15 structure
Deepak Singh
 
Chapter26 inheritance-ii
Deepak Singh
 
Chapter19 constructor-and-destructor
Deepak Singh
 
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Chapter20 class-example-program
Deepak Singh
 
Chapter25 inheritance-i
Deepak Singh
 
Chapter23 friend-function-friend-class
Deepak Singh
 
Revision notes for exam 2011 computer science with C++
Deepak Singh
 
+2 Computer Science - Volume II Notes
Andrew Raj
 
Ad

Similar to Chapter16 pointer (20)

PPT
Lecture2.ppt
Sabaunnisa3
 
PPT
8 Pointers
Praveen M Jigajinni
 
PPTX
pointers in c programming - example programs
stalin721831
 
PPT
Pointer
manish840
 
PPTX
COM1407: Working with Pointers
Hemantha Kulathilake
 
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
ab11167
 
PDF
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
PPT
Link list
Malainine Zaid
 
PDF
VIT351 Software Development VI Unit3
YOGESH SINGH
 
DOCX
programing fundamentals complete solutions.docx
ZAMANYousufzai1
 
PDF
C++_notes.pdf
HimanshuSharma997566
 
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPTX
Pointers and Dynamic Memory Allocation
Rabin BK
 
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
PDF
Pointers
Prasadu Peddi
 
PDF
Module 02 Pointers in C
Tushar B Kute
 
PPTX
Pointers in c
CHANDAN KUMAR
 
PPTX
pointers.pptx
janithlakshan1
 
Lecture2.ppt
Sabaunnisa3
 
pointers in c programming - example programs
stalin721831
 
Pointer
manish840
 
COM1407: Working with Pointers
Hemantha Kulathilake
 
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
ab11167
 
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
Link list
Malainine Zaid
 
VIT351 Software Development VI Unit3
YOGESH SINGH
 
programing fundamentals complete solutions.docx
ZAMANYousufzai1
 
C++_notes.pdf
HimanshuSharma997566
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers and Dynamic Memory Allocation
Rabin BK
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
ssuser951fc8
 
Pointers
Prasadu Peddi
 
Module 02 Pointers in C
Tushar B Kute
 
Pointers in c
CHANDAN KUMAR
 
pointers.pptx
janithlakshan1
 
Ad

More from Deepak Singh (17)

PDF
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
PDF
Chapter28 data-file-handling
Deepak Singh
 
PDF
Chapter18 class-and-objects
Deepak Singh
 
PDF
Chapter13 two-dimensional-array
Deepak Singh
 
PDF
Chapter12 array-single-dimension
Deepak Singh
 
PDF
Chapter 11 Function
Deepak Singh
 
PDF
Chapter 10 Library Function
Deepak Singh
 
PDF
Chapter 9 - Loops in C++
Deepak Singh
 
PDF
Chapter 8 - Conditional Statement
Deepak Singh
 
PDF
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
PDF
Chapter 5 - Operators in C++
Deepak Singh
 
PDF
Chapter 3 - Variable Memory Concept
Deepak Singh
 
PDF
Chapter 2 - Structure of C++ Program
Deepak Singh
 
PPT
Inheritance polymorphism-in-java
Deepak Singh
 
PDF
Computer science-2010-cbse-question-paper
Deepak Singh
 
PDF
Cbse question-paper-computer-science-2009
Deepak Singh
 
PDF
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
Computer networks - CBSE New Syllabus (083) Class - XII
Deepak Singh
 
Chapter28 data-file-handling
Deepak Singh
 
Chapter18 class-and-objects
Deepak Singh
 
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter12 array-single-dimension
Deepak Singh
 
Chapter 11 Function
Deepak Singh
 
Chapter 10 Library Function
Deepak Singh
 
Chapter 9 - Loops in C++
Deepak Singh
 
Chapter 8 - Conditional Statement
Deepak Singh
 
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
Chapter 5 - Operators in C++
Deepak Singh
 
Chapter 3 - Variable Memory Concept
Deepak Singh
 
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Inheritance polymorphism-in-java
Deepak Singh
 
Computer science-2010-cbse-question-paper
Deepak Singh
 
Cbse question-paper-computer-science-2009
Deepak Singh
 
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 

Chapter16 pointer

  • 1. www.cppforschool.com Pointer Accessing address of a variable Computer‟s memory is organized as a linear collection of bytes. Every byte in the computer‟s memory has an address. Each variable in program is stored at a unique address. We can use address operator & to get address of a variable: int num = 23; cout << &num; // prints address in hexadecimal POINTER A pointer is a variable that holds a memory address, usually the location of another variable in memory. Defining a Pointer Variable int *iptr; iptr can hold the address of an int Pointer Variables Assignment: int num = 25; int *iptr; iptr = &num; Memory layout To access num using iptr and indirection operator * cout << iptr; // prints 0x4a00 cout << *itptr; // prints 25 Similary, following declaration shows: char *cptr; float *fptr; cptr is a pointer to character and fptr is a pointer to float value.
  • 2. Pointer Arithmetic Some arithmetic operators can be used with pointers: - Increment and decrement operators ++, -- - Integers can be added to or subtracted from pointers using the operators +, -, +=, and -= Each time a pointer is incremented by 1, it points to the memory location of the next element of its base type. If “p” is a character pointer then “p++” will increment “p” by 1 byte. If “p” were an integer pointer its value on “p++” would be incremented by 4 bytes. Pointers and Arrays Array name is base address of array int vals[] = {4, 7, 11}; cout << vals; // displays 0x4a00 cout << vals[0]; // displays 4 Lets takes an example: int arr[]={4,7,11}; int *ptr = arr; What is ptr + 1? It means (address in ptr) + (1 * size of an int) cout << *(ptr+1); // displays 7 cout << *(ptr+2); // displays 11 Array Access Array notation arr[i] is equivalent to the pointer notation *(arr + i) Assume the variable definitions int arr[]={4,7,11}; int *ptr = arr; Examples of use of ++ and -- ptr++; // points at 7 ptr--; // now points at 4
  • 3. Character Pointers and Strings Initialize to a character string. char* a = “Hello”; a is pointer to the memory location where „H‟ is stored. Here “a” can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location. char* a = “Hello”; a gives address of „H‟ *a gives „H‟ a[0] gives „H‟ a++ gives address of „e‟ *a++ gives „e‟ Pointers as Function Parameters A pointer can be a parameter. It works like a reference parameter to allow change to argument from within function #include<iostream> using namespace std; void swap(int *, int *); int main() { int a=10,b=20; swap(&a, &b); cout<<a<<" "<<b; return 0; } void swap(int *x, int *y) { int t; t=*x; *x=*y; *y=t; } output: 20 10
  • 4. Pointers to Constants and Constant Pointers Pointer to a constant: cannot change the value that is pointed at Constant pointer: address in pointer cannot change once pointer is initialized Pointers to Structures We can create pointers to structure variables struct Student {int rollno; float fees;}; Student stu1; Student *stuPtr = &stu1; (*stuPtr).rollno= 104; -or- Use the form ptr->member: stuPtr->rollno = 104; Static allocation of memory In the static memory allocation, the amount of memory to be allocated is predicted and preknown. This memory is allocated during the compilation itself. All the declared variables declared normally, are allocated memory statically. Dynamic allocation of memory In the dynamic memory allocation, the amount of memory to be allocated is not known. This memory is allocated during run-time as and when required. The memory is dynamically allocated using new operator. 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. Dynamic Memory Allocation We can allocate storage for a variable while program is running by using new operator
  • 5. To allocate memory of type integer int *iptr=new int; To allocate array double *dptr = new double[25]; To allocate dynamic structure variables or objects Student sptr = new Student; //Student is tag name of structure Releasing Dynamic Memory Use delete to free dynamic memory delete iptr; To free dynamic array memory delete [] dptr; To free dynamic structure delete Student; Memory Leak If the objects, that are allocated memory dynamically, are not deleted using delete, the memory block remains occupied even at the end of the program. Such memory blocks are known as orphaned memory blocks. These orphaned memory blocks when increase in number, bring adverse effect on the system. This situation is called memory leak Self Referential Structure The self referential structures are structures that include an element that is a pointer to another structure of the same type. struct node { int data; node* next; }