SlideShare a Scribd company logo
Pointers and Arrays
Computer Programming (C++)Pointers and Arrays
Modifying Variables Using Pointers
4
Pointers
#include “timetype.h”
:
enum ColorType {RED, GREEN, BLUE};
struct PatientRec
{
int idNum;
int height;
int weight;
};
int alpha;
ColorType color;
PatientRec Patient;
TimeType startTime (8,30,0);
int* intPtr=α
ColorType* ColorPtr=&color;
PatientRec* PatientPtr=&patient;
TimeType* timePtr=&startTime;
(*patientPtr).idNum;
(*patientPtr).height;
(*patientPtr).weight;
PointerExpression->MemberName
(*PointerExpression).MemberName
The general rule for selection
Dot operator if the first operand
denotes: struct, class or union variable
Arrow Operator if the first operand
denotes: a pointer to a struct, class, or
union variable.
5
6
Constant pointer vs. Pointer to
constant
A constant pointer is declared using *const:
base type *const pointer name;
It is an error to change the constant pointer’s
value; but the value of object this constant
pointer points to can be changed.
A pointer to constant is declared as:
const base type *pointer name;
It is ok to change the value of the pointer, but
value of the constant which is referred to by
the pointer can not be changed.
7
Constant pointer vs. Pointer to
constant
Example 1:
int x = 0, y = 0;
const int * ptr1 = &x; // pointer to a constant
int * ptr2 = &x;
ptr1 = &y; // ok: can change ptr1
*ptr1 = 5; // error: can't change referent object
ptr2 = &y; // ok
*ptr2 = 5; // ok
8
Constant pointer vs. Pointer to
constant
Example 2:
int * const ptr = &x; // constant pointer
*ptr = 5; // ok : can change what ptr
points to
ptr = &y; // error: can't change ptr
9
Pointers and Dynamic Memory Allocation
Static memory allocation:
Example:
int x;
double y = 12.3456;
int a[10];
The memory for the object is allocated by the compiler
at the compile time. These objects are named
variables on the stack at runtime.
10
Pointers and Dynamic Memory Allocation
Dynamic memory allocation:
The memory allocation is done at run-time. The objects
are unnamed variables on the heap manipulated
indirectly through pointers.
11
Figure of Memory Allocation
dynamic objects memory managed by
programmer;by explicitly
call of new/delete
Function parameters,
Local objects memory managed by
compiler; used for each
function call
global objects
global data:object locations
are fixed value but can be
changed
Function definitions
code and read-only
objects
12
13

More Related Content

PPTX
C language presentation
PPTX
Pointer in c program
PPT
Pointers (Pp Tminimizer)
PPT
Pointers
PPT
Pointers in C
PPTX
Pointers in c++
PPTX
Double pointer (pointer to pointer)
PPTX
Pointers in c v5 12102017 1
C language presentation
Pointer in c program
Pointers (Pp Tminimizer)
Pointers
Pointers in C
Pointers in c++
Double pointer (pointer to pointer)
Pointers in c v5 12102017 1

What's hot (19)

ODP
C workshop day 7
PPTX
2CPP18 - Modifiers
PPT
C++ Pointers And References
PPTX
Presentation on pointer.
PPT
Pointers in c
PPTX
C programming - Pointer and DMA
PPTX
Pointers in c++
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Pointer to function 1
PPTX
Pointer in c
PDF
Pointer in c++ part1
PPTX
Pointers in C/C++ Programming
ODP
Pointers in c++ by minal
PPTX
Fundamentals of Pointers in C
PPTX
Function Pointer
PPTX
Pointers in c++
PPTX
Pointers in c - Mohammad Salman
PDF
Pointer in c++ part2
PPTX
Chp3(pointers ref)
C workshop day 7
2CPP18 - Modifiers
C++ Pointers And References
Presentation on pointer.
Pointers in c
C programming - Pointer and DMA
Pointers in c++
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointer to function 1
Pointer in c
Pointer in c++ part1
Pointers in C/C++ Programming
Pointers in c++ by minal
Fundamentals of Pointers in C
Function Pointer
Pointers in c++
Pointers in c - Mohammad Salman
Pointer in c++ part2
Chp3(pointers ref)
Ad

Similar to Computer Programming (C++)Pointers and Arrays (20)

PPTX
Lab 3.pptx
PPTX
Lab 3.pptx
PDF
Chapter 2 - Introduction to Pointer Variables - Student.pdf
PDF
Chapter 5 (Part I) - Pointers.pdf
PPTX
C++ Pointer | Introduction to programming
PPTX
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
PDF
0-Slot11-12-Pointers.pdf
PPT
Pointers definition syntax structure use
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PPT
Data structure and problem solving ch05.ppt
PDF
Pointers andmemory
PPTX
Mca ii dfs u-2 array records and pointer
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
PPTX
Pointers in c language
PDF
PPT
Pointers_in_c.pptfffgfggdggffffrreeeggttr
PPT
C/C++ Pointers explanationwith different examples
PPTX
Pointer in C++
Lab 3.pptx
Lab 3.pptx
Chapter 2 - Introduction to Pointer Variables - Student.pdf
Chapter 5 (Part I) - Pointers.pdf
C++ Pointer | Introduction to programming
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
0-Slot11-12-Pointers.pdf
Pointers definition syntax structure use
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Data structure and problem solving ch05.ppt
Pointers andmemory
Mca ii dfs u-2 array records and pointer
358 33 powerpoint-slides_3-pointers_chapter-3
Pointers in c language
Pointers_in_c.pptfffgfggdggffffrreeeggttr
C/C++ Pointers explanationwith different examples
Pointer in C++
Ad

More from AhmedToheed3 (6)

PPTX
Business statistics (Basic concepts of Index Numbers)
PPTX
Business statistics (Shape of data)
PPTX
Business statistics ( Methods of variablity measurement))
PPTX
Business statistics (Methods of Centre Measurement)
PPT
Business statistics (Basics)
PPT
Business statistics (Introduction)
Business statistics (Basic concepts of Index Numbers)
Business statistics (Shape of data)
Business statistics ( Methods of variablity measurement))
Business statistics (Methods of Centre Measurement)
Business statistics (Basics)
Business statistics (Introduction)

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
NewMind AI Weekly Chronicles - August'25 Week I

Computer Programming (C++)Pointers and Arrays

  • 4. 4 Pointers #include “timetype.h” : enum ColorType {RED, GREEN, BLUE}; struct PatientRec { int idNum; int height; int weight; }; int alpha; ColorType color; PatientRec Patient; TimeType startTime (8,30,0); int* intPtr=α ColorType* ColorPtr=&color; PatientRec* PatientPtr=&patient; TimeType* timePtr=&startTime; (*patientPtr).idNum; (*patientPtr).height; (*patientPtr).weight; PointerExpression->MemberName (*PointerExpression).MemberName The general rule for selection Dot operator if the first operand denotes: struct, class or union variable Arrow Operator if the first operand denotes: a pointer to a struct, class, or union variable.
  • 5. 5
  • 6. 6 Constant pointer vs. Pointer to constant A constant pointer is declared using *const: base type *const pointer name; It is an error to change the constant pointer’s value; but the value of object this constant pointer points to can be changed. A pointer to constant is declared as: const base type *pointer name; It is ok to change the value of the pointer, but value of the constant which is referred to by the pointer can not be changed.
  • 7. 7 Constant pointer vs. Pointer to constant Example 1: int x = 0, y = 0; const int * ptr1 = &x; // pointer to a constant int * ptr2 = &x; ptr1 = &y; // ok: can change ptr1 *ptr1 = 5; // error: can't change referent object ptr2 = &y; // ok *ptr2 = 5; // ok
  • 8. 8 Constant pointer vs. Pointer to constant Example 2: int * const ptr = &x; // constant pointer *ptr = 5; // ok : can change what ptr points to ptr = &y; // error: can't change ptr
  • 9. 9 Pointers and Dynamic Memory Allocation Static memory allocation: Example: int x; double y = 12.3456; int a[10]; The memory for the object is allocated by the compiler at the compile time. These objects are named variables on the stack at runtime.
  • 10. 10 Pointers and Dynamic Memory Allocation Dynamic memory allocation: The memory allocation is done at run-time. The objects are unnamed variables on the heap manipulated indirectly through pointers.
  • 11. 11 Figure of Memory Allocation dynamic objects memory managed by programmer;by explicitly call of new/delete Function parameters, Local objects memory managed by compiler; used for each function call global objects global data:object locations are fixed value but can be changed Function definitions code and read-only objects
  • 12. 12
  • 13. 13