SlideShare a Scribd company logo
1 of 25
Name – Amir hamza khan 
Instructor –Sir Irfan Latif Memon 
Section – BSCS-1 
RegNo. – 1312118
 Introduction to pointers. 
 The addresss of operator “&”. 
 Pointers and the “*” operator. 
 Pointers and function parameters. 
 Size of operators. 
Relation between array and pointers.
 The pointer is a variable that holds a memory address. 
 To understand the pointers we should first understand a bit 
about the computer memory. 
 Whenever any application is executed, it is loaded in to the 
memory form the disk and all the elements and components of 
that application will be located at the particular location 
somewhere in the memory.
 Computer memory is divided into sequentially numbered memory 
locations and each variable is located at a unique location in 
memory, known as its address. 
 The address of these memory locations is typically represented in 
Hexadecimal format with the prefix 0x before the number (e.g. 
0x8f4ffff2).
 As you know every variable is a memory location and every 
memory location has its address defined which can be accessed 
using ampersand (&) operator which denotes an address in 
memory 
 To return the address of the certain variables the address-of 
operator is placed in front of the variable name as, 
 cout<<&var1; 
 cout<<&var2; 
 cout<<&var3; 
 Now the “&” used with the variables in the cout statement will 
return the addresses at which these variable are located into the 
memory.
 #include <iostream.h> 
 main () 
 { 
 int var1; 
 char var2[10]; 
 cout << "Address of var1 
variable: "; 
 cout << &var1 << endl; 
 cout << "Address of var2 
variable: "; 
 cout << &var2 << endl; 
 }
 A pointer is a variable whose value is the address of another 
variable. Like any variable or constant, you must declare a 
pointer before you can work with it. The general form of a 
pointer variable declaration is: 
 Type *var-name; 
 Here, type is the pointer's base type; it must be a valid C++ type 
and var-name is the name of the pointer variable. The asterisk 
you used to declare a pointer is the same asterisk that you use 
for multiplication. However, in this statement the asterisk is 
being used to designate a variable as a pointer.
 int *ip; // pointer to an integer 
 double *dp; // pointer to a double 
 float *fp; // pointer to a float 
 char *ch; // pointer to character 
 The actual data type of the value of all pointers, whether 
integer, float, character, or otherwise, is the same, a long 
hexadecimal number that represents a memory address. The only 
difference between pointers of different data types is the data 
type of the variable or constant that the pointer points to
 It is always a good practice to assign the pointer NULL to a 
pointer variable in case you do not have exact address to be 
assigned. This is done at the time of variable declaration. A 
pointer that is assigned NULL is called a null pointer
 #include <iostream.h> 
 main () 
 { 
 int *ptr = NULL; 
 Cout<<“the value of ptr 
is” <<ptr<<endl; 
 
 }
 Example: Function to swap 
the values of two variables 
 #include <iostream.h> 
 void swap2(int* a, int* b) 
 { 
 int tmp; 
 tmp = *a; 
 *a = *b; 
 *b = tmp; 
 return; 
 } 
 int main() 
 { 
 int x = 1, y = 2; 
 swap2(&x, &y); 
 cout<<x<<y; 
 return 0; 
 }
 Pointers can be used to pass addresses of variables to called 
functions, thus allowing the called function to alter the values 
stored there. 
 We looked earlier at a swap function that did not change the 
values stored in the main program because only the values were 
passed to the function swap. 
 This is known as "call by value".
 If instead of passing the values of the variables to the called 
function, we pass their addresses, so that the called function 
can change the values stored in the calling routine. This is 
known as "call by reference" since we are referencing the 
variables. 
 The following shows the swap function modified from a "call by 
value" to a "call by reference". Note that the values are now 
actually swapped when the control is returned to main 
function.
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type. 
 The syntax of using sizeof is : 
 sizeof(data type) 
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type.
#include <iostream.h> 
main() 
{ 
cout << "Size of char : " << sizeof(char) << endl; 
cout << "Size of int : " << sizeof(int) << endl; 
cout << "Size of short int : " << sizeof(short int) << endl; 
cout << "Size of long int : " << sizeof(long int) << endl; 
cout << "Size of float : " << sizeof(float) << endl; 
cout << "Size of double : " << sizeof(double) << endl; 
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; 
}
 Arrays and pointers are closely related 
◦ Array name is like constant pointer 
◦ Pointers can do array subscripting operations
 Accessing array elements with pointers 
◦ Assume declarations: 
int b[ 5 ]; 
int *bPtr; 
bPtr = b; 
◦ Element b[ n ] can be accessed by *( bPtr + n ) 
 Called pointer/offset notation 
◦ Addresses 
 &b[ 3 ] is same as bPtr + 3 
◦ Array name can be treated as pointer 
 b[ 3 ] is same as *( b + 3 ) 
◦ Pointers can be subscripted (pointer/subscript notation) 
 bPtr[ 3 ] is same as b[ 3 ]
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)

More Related Content

What's hot (20)

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers
PointersPointers
Pointers
 
pointers
pointerspointers
pointers
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Ponters
PontersPonters
Ponters
 
Pointer
PointerPointer
Pointer
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 

Viewers also liked

Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functionsVineeta Garg
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraintsVineeta Garg
 
Computer science study material
Computer science study materialComputer science study material
Computer science study materialSwarup Kumar Boro
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Aman Deep
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++Vineeta Garg
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Examshishamrizvi
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Roman Rodomansky
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 

Viewers also liked (20)

Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
SQL
SQLSQL
SQL
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
stack & queue
stack & queuestack & queue
stack & queue
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 

Similar to c++ pointers by Amir Hamza Khan (SZABISTIAN)

Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersRai University
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointersRai University
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3sumitbardhan
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C languageMehwish Mehmood
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 

Similar to c++ pointers by Amir Hamza Khan (SZABISTIAN) (20)

Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C language presentation
C language presentationC language presentation
C language presentation
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 

Recently uploaded

main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 

c++ pointers by Amir Hamza Khan (SZABISTIAN)

  • 1.
  • 2. Name – Amir hamza khan Instructor –Sir Irfan Latif Memon Section – BSCS-1 RegNo. – 1312118
  • 3.  Introduction to pointers.  The addresss of operator “&”.  Pointers and the “*” operator.  Pointers and function parameters.  Size of operators. Relation between array and pointers.
  • 4.  The pointer is a variable that holds a memory address.  To understand the pointers we should first understand a bit about the computer memory.  Whenever any application is executed, it is loaded in to the memory form the disk and all the elements and components of that application will be located at the particular location somewhere in the memory.
  • 5.  Computer memory is divided into sequentially numbered memory locations and each variable is located at a unique location in memory, known as its address.  The address of these memory locations is typically represented in Hexadecimal format with the prefix 0x before the number (e.g. 0x8f4ffff2).
  • 6.  As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory  To return the address of the certain variables the address-of operator is placed in front of the variable name as,  cout<<&var1;  cout<<&var2;  cout<<&var3;  Now the “&” used with the variables in the cout statement will return the addresses at which these variable are located into the memory.
  • 7.  #include <iostream.h>  main ()  {  int var1;  char var2[10];  cout << "Address of var1 variable: ";  cout << &var1 << endl;  cout << "Address of var2 variable: ";  cout << &var2 << endl;  }
  • 8.  A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is:  Type *var-name;  Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
  • 9.  int *ip; // pointer to an integer  double *dp; // pointer to a double  float *fp; // pointer to a float  char *ch; // pointer to character  The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to
  • 10.  It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer
  • 11.  #include <iostream.h>  main ()  {  int *ptr = NULL;  Cout<<“the value of ptr is” <<ptr<<endl;   }
  • 12.  Example: Function to swap the values of two variables  #include <iostream.h>  void swap2(int* a, int* b)  {  int tmp;  tmp = *a;  *a = *b;  *b = tmp;  return;  }  int main()  {  int x = 1, y = 2;  swap2(&x, &y);  cout<<x<<y;  return 0;  }
  • 13.  Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.  We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap.  This is known as "call by value".
  • 14.  If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables.  The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.
  • 15.  The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.  The syntax of using sizeof is :  sizeof(data type)  The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
  • 16. #include <iostream.h> main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; }
  • 17.
  • 18.  Arrays and pointers are closely related ◦ Array name is like constant pointer ◦ Pointers can do array subscripting operations
  • 19.  Accessing array elements with pointers ◦ Assume declarations: int b[ 5 ]; int *bPtr; bPtr = b; ◦ Element b[ n ] can be accessed by *( bPtr + n )  Called pointer/offset notation ◦ Addresses  &b[ 3 ] is same as bPtr + 3 ◦ Array name can be treated as pointer  b[ 3 ] is same as *( b + 3 ) ◦ Pointers can be subscripted (pointer/subscript notation)  bPtr[ 3 ] is same as b[ 3 ]