SlideShare a Scribd company logo
1 of 14
Programming fundamentals 2
Chapter 3:Pointer
Miss:Hanan Hardam
1
Computer Skills2 for Scientific
Colleges
2
Pointers in C++
Topics to cover:
• Overview of Pointers
• Pointer Declaration
• Pointer Assignment
• Pointer Arithmetic
• Relations Between Pointers and Arrays
• Pointers and Strings
Computer Skills2 for Scientific
Colleges
3
Overview of Pointers
• A Pointer in C++ is variable whose value is a memory
address.
• With pointers many memory locations can be
referenced.
• Some data structures use pointers (e.g. linked list,
tree).
• The * and & operators
- & operator is the address operator
- * operator is the dereferencing operator. It is used in
pointers declaration
Computer Skills2 for Scientific
Colleges
4
Pointer Declaration
• Pointers are declared as follows:
<type> * variable_name ;
e.g.
int * xPtr; // xPtr is a pointer to data of type integer
char * cPtr; //cPtr is a pointer to data of type character
void * yPtr; // yPtr is a generic pointer,
// represents any type
Computer Skills2 for Scientific
Colleges
5
Pointer Assignment
• Assignment can be applied on pointers of the same type
• If not the same type, a cast operator must be used
• Exception: pointer to void does not need casting to convert a
pointer to void type
• void pointers cannot be dereferenced
• Example
int *xPtr, *yPtr; int x = 5;
…
xPtr = & x; // xPtr now points to address of x
yPtr = xPtr; // now yPtr and xPtr point to x
Computer Skills2 for Scientific
Colleges
6
Pointer Arithmetic
• Increment / decrement pointers ( ++ or -- )
• Add / subtract an integer to/from a pointer
( + or += , - or -= )
• Pointers may be subtracted from each other
• Pointer arithmetic is meaningless unless
performed on an array
Computer Skills2 for Scientific
Colleges
7
Pointer Arithmetic (Cont.)
• Example
Consider an integer array of 5 elements on a machine using 4
bytes for integers.
1000 1004 1008 1012 1016
Pointer variable vPtr
- vPtr pointes to first element V[0] (location 1000)
i.e. vPtr = 1000
- vPtr +=2; sets vPtr to 1008
i.e. vPtr points to V[2]
V[0] V[1] V[2] V[3] V[4]
Computer Skills2 for Scientific
Colleges
8
Pointer Arithmetic (Cont.)
• Subtracting pointers
- Returns the number of elements between two
addresses
e.g. if v is an array and
vPtr1 = v[0];
vPtr2 = v[2];
then vPtr2 – vPtr1 = 2 (i.e. 2 addresses)
Computer Skills2 for Scientific
Colleges
9
Relations Between Pointers and Arrays
• Arrays and pointers are closely related.
- Array name is like constant pointer
- Pointers can do array subscribing operations
- If we declare an array A[4] and a pointer aPtr
 aPtr is equal to A
aPtr == A
 aPtr is equal to the address of the first
element of A
aPtr == & A[0]
Computer Skills2 for Scientific
Colleges
10
Relations Between Pointers and Arrays (Cont.)
• Accessing array elements with pointers:
- Element A[i] can be accessed by *(aPtr+i)
 This is called pointer/offset notation
- Array itself can use pointer arithmetic
 A[3] is same as *(A+3)
- Pointers can be subscripted
(i.e. pointer/subscript notation)
 aPtr [3] is same as A[3]
Computer Skills2 for Scientific
Colleges
11
Examples on Pointers
//File: swap.cpp
//A program to call a function to swap two numbers using reference parameters
#include <iostream.h>
void swap(int *, int *); // This is swap's prototype
void main()
{ int x = 5, y = 7;
swap(&x , &y); // calling swap with reference parameters
cout << "n x is now "<< x << " and y is now " << y << 'n';
}
// swap function is defined here using dereferencing operator ‘*’
void swap(int *a, int *b)
{ int temp;
temp = *a;
*a = *b;
*b = temp;
}
Computer Skills2 for Scientific
Colleges
12
Examples on Pointers (Cont.)
//File: pointers.cpp
//A program to test pointers and references
#include <iostream.h>
void main ( )
{ int intVar = 10;
int *intPtr; // intPtr is a pointer
intPtr = & intVar;
cout << "nLocation of intVar: " << & intVar;
cout << "nContents of intVar: " << intVar;
cout << "nLocation of intPtr: " << & intPtr;
cout << "nContents of intPtr: " << intPtr;
cout << "nThe value that intPtr points to: " << * intPtr;
}
Computer Skills2 for Scientific
Colleges
13
Pointers and Strings
• Pointers can be used to declare strings and with string
functions (see next lecture slides)
When calling, the pointer formal parameter will points to the
actual parameter.
#include <iostream.h>
void Increment(int*);
void main() {
int A = 10;
Increment(&A);
cout<<A<<endl;
}
void Increment(int *X) { ++*X; }
Computer Skills2 for Scientific
Colleges
14

More Related Content

Similar to Programming fundamentals 2:pointers in c++ clearly explained

Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in CTushar B Kute
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxhappycocoman
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnmit23cs
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)Mohd Effandi
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it outrajatryadav22
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 

Similar to Programming fundamentals 2:pointers in c++ clearly explained (20)

Pointers
PointersPointers
Pointers
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers
PointersPointers
Pointers
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dnU3_4_PointerArithmetic.pdf people will use the above preaentationa dn
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointer
PointerPointer
Pointer
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 

Recently uploaded

PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Programming fundamentals 2:pointers in c++ clearly explained

  • 1. Programming fundamentals 2 Chapter 3:Pointer Miss:Hanan Hardam 1
  • 2. Computer Skills2 for Scientific Colleges 2 Pointers in C++ Topics to cover: • Overview of Pointers • Pointer Declaration • Pointer Assignment • Pointer Arithmetic • Relations Between Pointers and Arrays • Pointers and Strings
  • 3. Computer Skills2 for Scientific Colleges 3 Overview of Pointers • A Pointer in C++ is variable whose value is a memory address. • With pointers many memory locations can be referenced. • Some data structures use pointers (e.g. linked list, tree). • The * and & operators - & operator is the address operator - * operator is the dereferencing operator. It is used in pointers declaration
  • 4. Computer Skills2 for Scientific Colleges 4 Pointer Declaration • Pointers are declared as follows: <type> * variable_name ; e.g. int * xPtr; // xPtr is a pointer to data of type integer char * cPtr; //cPtr is a pointer to data of type character void * yPtr; // yPtr is a generic pointer, // represents any type
  • 5. Computer Skills2 for Scientific Colleges 5 Pointer Assignment • Assignment can be applied on pointers of the same type • If not the same type, a cast operator must be used • Exception: pointer to void does not need casting to convert a pointer to void type • void pointers cannot be dereferenced • Example int *xPtr, *yPtr; int x = 5; … xPtr = & x; // xPtr now points to address of x yPtr = xPtr; // now yPtr and xPtr point to x
  • 6. Computer Skills2 for Scientific Colleges 6 Pointer Arithmetic • Increment / decrement pointers ( ++ or -- ) • Add / subtract an integer to/from a pointer ( + or += , - or -= ) • Pointers may be subtracted from each other • Pointer arithmetic is meaningless unless performed on an array
  • 7. Computer Skills2 for Scientific Colleges 7 Pointer Arithmetic (Cont.) • Example Consider an integer array of 5 elements on a machine using 4 bytes for integers. 1000 1004 1008 1012 1016 Pointer variable vPtr - vPtr pointes to first element V[0] (location 1000) i.e. vPtr = 1000 - vPtr +=2; sets vPtr to 1008 i.e. vPtr points to V[2] V[0] V[1] V[2] V[3] V[4]
  • 8. Computer Skills2 for Scientific Colleges 8 Pointer Arithmetic (Cont.) • Subtracting pointers - Returns the number of elements between two addresses e.g. if v is an array and vPtr1 = v[0]; vPtr2 = v[2]; then vPtr2 – vPtr1 = 2 (i.e. 2 addresses)
  • 9. Computer Skills2 for Scientific Colleges 9 Relations Between Pointers and Arrays • Arrays and pointers are closely related. - Array name is like constant pointer - Pointers can do array subscribing operations - If we declare an array A[4] and a pointer aPtr  aPtr is equal to A aPtr == A  aPtr is equal to the address of the first element of A aPtr == & A[0]
  • 10. Computer Skills2 for Scientific Colleges 10 Relations Between Pointers and Arrays (Cont.) • Accessing array elements with pointers: - Element A[i] can be accessed by *(aPtr+i)  This is called pointer/offset notation - Array itself can use pointer arithmetic  A[3] is same as *(A+3) - Pointers can be subscripted (i.e. pointer/subscript notation)  aPtr [3] is same as A[3]
  • 11. Computer Skills2 for Scientific Colleges 11 Examples on Pointers //File: swap.cpp //A program to call a function to swap two numbers using reference parameters #include <iostream.h> void swap(int *, int *); // This is swap's prototype void main() { int x = 5, y = 7; swap(&x , &y); // calling swap with reference parameters cout << "n x is now "<< x << " and y is now " << y << 'n'; } // swap function is defined here using dereferencing operator ‘*’ void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }
  • 12. Computer Skills2 for Scientific Colleges 12 Examples on Pointers (Cont.) //File: pointers.cpp //A program to test pointers and references #include <iostream.h> void main ( ) { int intVar = 10; int *intPtr; // intPtr is a pointer intPtr = & intVar; cout << "nLocation of intVar: " << & intVar; cout << "nContents of intVar: " << intVar; cout << "nLocation of intPtr: " << & intPtr; cout << "nContents of intPtr: " << intPtr; cout << "nThe value that intPtr points to: " << * intPtr; }
  • 13. Computer Skills2 for Scientific Colleges 13 Pointers and Strings • Pointers can be used to declare strings and with string functions (see next lecture slides)
  • 14. When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; } Computer Skills2 for Scientific Colleges 14