SlideShare une entreprise Scribd logo
1  sur  21
POINTER
U N I T 7
A U T H O R : A B D U L L A H J A N
MEMORY & REFERENCES
• Computer memory is a collection of different consecutive memory location. These memory location
are numbered sequentially. Each variable is created at a unique location in memory known as its
address.
• A program may declare many variable for different tasks. A variable declaration reserve specific
amount of space in memory for a particular variable. The variable name is used to refer to that
memory location. It allows the user to access a value in the memory. The computer refer to the
memory using an address. A variable declaration associates following three attributes to a variable:
• Variable name
• Variable type
• Variable memory address
• The following statement declares an integer variable:
• Int a;
• The name of variable is “a” and the type of variable is int. the address of the variable is unknown.
Computer create the variable at any available location in the memory. The memory address is a
hexadecimal number that refers to a particular location in the memory. The variable is created in the
memory as follow:
MEMORY & REFERENCES
• The above box represent the memory location allocated for the variable a. the hexadecimal value
above the box is its assumed address. The variable will occupy 2 bytes or 4 bytes in memory
depending on the type of computer.
• The actual address of the variable can be display by using reference operator &. It is also known as
address operator. The reference operator is used to access the memory address of a variable. The
following statement will display the address of a .
• Cout<<&a;
int
a
0*0064df0
MEMORY & REFERENCES
• #include<iostream>
• Using namespace std;
• Int main(){
• Int n = 10;
• Cout<<“the value of n:”<<n<<endl;
• Cout<<“the address of n “<<&n<<endl;
• Getch();
• }
Ouptut:
The value of n:10
The address of n:
0*8fb1fff4
POINTERS
• A pointer is a variable that is used to store a memory address. The reference operator
is used to access the memory address of a variable and store it in a pointer.
POINTER
• A pointer is a variable that holds the address of another variable. In memory each and
every variable has an address assigned to it by the compiler and if a programmer
wants to access that address another variable called pointer is needed. For the
declaration of pointer an asterisk* symbol is used between the data type and the
variable name.
• Consider the following pointers declaration of the integer variable marks, floating
point variable percentage and character type variable name
• Int *mark;
• Float *percentage;
• Char *name;
MEMORY ADDRESS
• When writing a program, variable need to be declared. Declaration of variable is simple
and its declaration tells the computer to reserve space in memory for this variable. The
name of the variable refers to that memory space. This task is automatically performed
by the operating system during runtime. When variable are declared programmers
store data in these variable. Computer refers to that space using an address. Therefore
everything a programmer declares has an address. It is analogous(same face) to the
home address of some person. Using pointer variable one can easily find out the
address of a particular variable.
REFERENCE OPERATOR &
• As pointers are the variable which hold the addresses of other variable therefore while
assigning addresses to them a programmer needs a special type of operator called
address-of operator that is denoted by ampersand & symbol. This provides address of
a memory location.
• To understand it consider the following segment of code
• Float x = 55;
• Float *fpointer;
• fPointer = &x; //assign address of x to pointer
1000 5.5
xfpointer
• Consider the following program
• Int main(){
• Float x = 55;
• Float *fPointer;
• fPointer = &x;
• Cout<<“the address of x = “<<&x<<endl;
• Cout<<“the value of x = “<<x<<endl;
• Cout<<“the value of fPointer =“<<fpointer;
• Getch();
• Return 0;
• }
• Ouptut :
• The address of x = 1000
• The value of x = 5.5
• The value of fPointer = 1000
• This program gives some other output (address) on another computer depending upon the
availability of the memory
DEREFERENCE OPERATOR*
• Pointer are used to store the address of another variable. If we want to store the value
of the variable through the pointer, then we need a special type of operator called
dereference operator denoted by asterisk*.
• Use of dereference (*) in a program
• Int main(){
• Int n = 100;
• Int *pn; //define a pointer to n
• Pn = &n; //pn store the address of n
• Int valueN;
• ValueN = *Pn;
• Cout<<“the address of n = “<<&n<<endl;
• Cout<<“the value of n = “<<n<<endl;
• Cout<<“the value of Pn = “<<Pn<<endl;
• Cout<<“the value of *Pn = “<<(*pn)<<endl;
• Cout<<“the value of value = “<<valueN;
• Getch();
• Return 0;
• }
• Output :
• The address of n = 0*8fc5fff4
• The value of n = 100
• The value of pn = 0*8fc5fff4
• The value of (*pn) = 100
• The value of valueN = 100
DECLARING VARIABLE OF POINTER
TYPE
• The declaration of pointer is simple and is similar to the declaration of a regular
variable with a minor difference of the use of an asterisk * symbol between the
datatype and the variable name.
• Consider the following general format
• Datatype * name variable;
• Example:
• Int* totalmarks;
• Char *name;
• Float * percentage;
VOID POINTER
• Usually the type of a variable and the type of pointer variable that holds memory address of the variable
must be the same. But the “void” type pointer variable can hold memory address of variable of any data
type. A void type pointer is declared by using the keyword “void” the asterisk is used before pointer variable
name.
• The keyword ‘void’ is used as the data type of the pointer as follows
• Void *p;
• The above statement declares a pointer variable p. the data type of the pointer is void. It means that it can
store the memory address of any type of variable.
• Int x;
• Float y;
• Void *pointervoid;
• Pointervoid = &x; // valid
• Pointervoid = &y; //valid
• In this segment of code pointerVoid store the address of both integer variable X and
floating point variable Y , the compiler decides at run time that the address of which
type of variable should be assigned to this pointer.
VOID POINTER
• Int main(){
• Int n= 10;
• Float f = 25.18;
• Char c = ‘$’;
• Void *ptr;
• Ptr = &n;
• Cout<<“the value of n:”<<n<<endl;
• Cout<<“the address of n:”<<ptr<<endl;
• Ptr = &f;
• Cout<<“the value of f:<<f<<endl;
• Cout<<“the address of f:”<<ptr<<endl;
• Ptr = &c;
• Cout<<“the value of c:”<<c<<endl;
• Cout<<“the address of c”<<ptr<<endl;
• Getch();
• }
Output
The value of n: 10
The address of n: 0*8f72
The value of f: 25.13
The address of f:0*f573
The value of c:$
The address of c: 0*f567
VOID POINTER
• The above program declares and initializes three variable of type int , float and char. It
also declare a void pointer variable. The pointer can refer to different types of variables
. The program stores the memory addresses of the variables n,f and c one and displays
the values and memory addresses of these variables
POINTER INITIALIZATION
• Assigning values to pointer at declaration time is called pointer initialization.
• Example
• Float temperature;
• Float *Ptemperaturre = &temperature;
• Or
• Float temperature;
• Float *Ptemperaturre;
• Ptemperature = &temperature;
POINTER INITIALIZATION
• It should be considered that at the moment of declaring a pointer, the asterisk *
indicates that it is a pointer variable and not the dereference operator.
POINTER INITIALIZATION
• Consider the following program to explain the concept of pointer initialization
• Int main(){
• Float temperature;
• Float *Ptemperature = &temperature;
• Cout<<“the address of temperature is =“<<&temperature<<endl;
• Cout<<“the value of *Ptemperature is = “<<Ptemperature<<endl;
• Getch();
• Return 0;
• }
POINTER INITIALIZATION
• Here the pointer Ptemperature is initialized with the address of the variable
temperature.
• Sometime we need to initialize a pointer to zero. Such pointers are called null pointers
and they do not point to anything. Null pointer can be defined by assigning address 0
to them.
• Consider the following initialization to demonstrate null pointer
• Int *NPtr; // define null pointer
• NPtr = 0; // this assigns address 0 to NPtr
• the use of Null pointers is mostly done in dynamic memory allocation

Contenu connexe

Tendances (20)

8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
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
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
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
C PointersC Pointers
C Pointers
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers
PointersPointers
Pointers
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Ponters
PontersPonters
Ponters
 
Pointer
PointerPointer
Pointer
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
C pointers
C pointersC pointers
C pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 

Similaire à Used of Pointer in C++ Programming

Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)Mohd Effandi
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxsangeeta borde
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
INTERMEDIATE PROGRAMMING POINTERS IN C.pptx
INTERMEDIATE PROGRAMMING POINTERS IN C.pptxINTERMEDIATE PROGRAMMING POINTERS IN C.pptx
INTERMEDIATE PROGRAMMING POINTERS IN C.pptxstephkyrie
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
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
 
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
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
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
 

Similaire à Used of Pointer in C++ Programming (20)

Session 5
Session 5Session 5
Session 5
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
Pointers
PointersPointers
Pointers
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointer
PointerPointer
Pointer
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
INTERMEDIATE PROGRAMMING POINTERS IN C.pptx
INTERMEDIATE PROGRAMMING POINTERS IN C.pptxINTERMEDIATE PROGRAMMING POINTERS IN C.pptx
INTERMEDIATE PROGRAMMING POINTERS IN C.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers
PointersPointers
Pointers
 
Pointer
PointerPointer
Pointer
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
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
 
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...
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
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
 

Dernier

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Dernier (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Used of Pointer in C++ Programming

  • 1. POINTER U N I T 7 A U T H O R : A B D U L L A H J A N
  • 2. MEMORY & REFERENCES • Computer memory is a collection of different consecutive memory location. These memory location are numbered sequentially. Each variable is created at a unique location in memory known as its address. • A program may declare many variable for different tasks. A variable declaration reserve specific amount of space in memory for a particular variable. The variable name is used to refer to that memory location. It allows the user to access a value in the memory. The computer refer to the memory using an address. A variable declaration associates following three attributes to a variable: • Variable name • Variable type • Variable memory address • The following statement declares an integer variable: • Int a; • The name of variable is “a” and the type of variable is int. the address of the variable is unknown. Computer create the variable at any available location in the memory. The memory address is a hexadecimal number that refers to a particular location in the memory. The variable is created in the memory as follow:
  • 3. MEMORY & REFERENCES • The above box represent the memory location allocated for the variable a. the hexadecimal value above the box is its assumed address. The variable will occupy 2 bytes or 4 bytes in memory depending on the type of computer. • The actual address of the variable can be display by using reference operator &. It is also known as address operator. The reference operator is used to access the memory address of a variable. The following statement will display the address of a . • Cout<<&a; int a 0*0064df0
  • 4. MEMORY & REFERENCES • #include<iostream> • Using namespace std; • Int main(){ • Int n = 10; • Cout<<“the value of n:”<<n<<endl; • Cout<<“the address of n “<<&n<<endl; • Getch(); • } Ouptut: The value of n:10 The address of n: 0*8fb1fff4
  • 5. POINTERS • A pointer is a variable that is used to store a memory address. The reference operator is used to access the memory address of a variable and store it in a pointer.
  • 6. POINTER • A pointer is a variable that holds the address of another variable. In memory each and every variable has an address assigned to it by the compiler and if a programmer wants to access that address another variable called pointer is needed. For the declaration of pointer an asterisk* symbol is used between the data type and the variable name. • Consider the following pointers declaration of the integer variable marks, floating point variable percentage and character type variable name • Int *mark; • Float *percentage; • Char *name;
  • 7. MEMORY ADDRESS • When writing a program, variable need to be declared. Declaration of variable is simple and its declaration tells the computer to reserve space in memory for this variable. The name of the variable refers to that memory space. This task is automatically performed by the operating system during runtime. When variable are declared programmers store data in these variable. Computer refers to that space using an address. Therefore everything a programmer declares has an address. It is analogous(same face) to the home address of some person. Using pointer variable one can easily find out the address of a particular variable.
  • 8. REFERENCE OPERATOR & • As pointers are the variable which hold the addresses of other variable therefore while assigning addresses to them a programmer needs a special type of operator called address-of operator that is denoted by ampersand & symbol. This provides address of a memory location. • To understand it consider the following segment of code • Float x = 55; • Float *fpointer; • fPointer = &x; //assign address of x to pointer 1000 5.5 xfpointer
  • 9. • Consider the following program • Int main(){ • Float x = 55; • Float *fPointer; • fPointer = &x; • Cout<<“the address of x = “<<&x<<endl; • Cout<<“the value of x = “<<x<<endl; • Cout<<“the value of fPointer =“<<fpointer; • Getch(); • Return 0; • } • Ouptut : • The address of x = 1000 • The value of x = 5.5 • The value of fPointer = 1000 • This program gives some other output (address) on another computer depending upon the availability of the memory
  • 10. DEREFERENCE OPERATOR* • Pointer are used to store the address of another variable. If we want to store the value of the variable through the pointer, then we need a special type of operator called dereference operator denoted by asterisk*.
  • 11. • Use of dereference (*) in a program • Int main(){ • Int n = 100; • Int *pn; //define a pointer to n • Pn = &n; //pn store the address of n • Int valueN; • ValueN = *Pn; • Cout<<“the address of n = “<<&n<<endl; • Cout<<“the value of n = “<<n<<endl; • Cout<<“the value of Pn = “<<Pn<<endl; • Cout<<“the value of *Pn = “<<(*pn)<<endl; • Cout<<“the value of value = “<<valueN; • Getch(); • Return 0; • }
  • 12. • Output : • The address of n = 0*8fc5fff4 • The value of n = 100 • The value of pn = 0*8fc5fff4 • The value of (*pn) = 100 • The value of valueN = 100
  • 13. DECLARING VARIABLE OF POINTER TYPE • The declaration of pointer is simple and is similar to the declaration of a regular variable with a minor difference of the use of an asterisk * symbol between the datatype and the variable name. • Consider the following general format • Datatype * name variable; • Example: • Int* totalmarks; • Char *name; • Float * percentage;
  • 14. VOID POINTER • Usually the type of a variable and the type of pointer variable that holds memory address of the variable must be the same. But the “void” type pointer variable can hold memory address of variable of any data type. A void type pointer is declared by using the keyword “void” the asterisk is used before pointer variable name. • The keyword ‘void’ is used as the data type of the pointer as follows • Void *p; • The above statement declares a pointer variable p. the data type of the pointer is void. It means that it can store the memory address of any type of variable. • Int x; • Float y; • Void *pointervoid; • Pointervoid = &x; // valid • Pointervoid = &y; //valid
  • 15. • In this segment of code pointerVoid store the address of both integer variable X and floating point variable Y , the compiler decides at run time that the address of which type of variable should be assigned to this pointer.
  • 16. VOID POINTER • Int main(){ • Int n= 10; • Float f = 25.18; • Char c = ‘$’; • Void *ptr; • Ptr = &n; • Cout<<“the value of n:”<<n<<endl; • Cout<<“the address of n:”<<ptr<<endl; • Ptr = &f; • Cout<<“the value of f:<<f<<endl; • Cout<<“the address of f:”<<ptr<<endl; • Ptr = &c; • Cout<<“the value of c:”<<c<<endl; • Cout<<“the address of c”<<ptr<<endl; • Getch(); • } Output The value of n: 10 The address of n: 0*8f72 The value of f: 25.13 The address of f:0*f573 The value of c:$ The address of c: 0*f567
  • 17. VOID POINTER • The above program declares and initializes three variable of type int , float and char. It also declare a void pointer variable. The pointer can refer to different types of variables . The program stores the memory addresses of the variables n,f and c one and displays the values and memory addresses of these variables
  • 18. POINTER INITIALIZATION • Assigning values to pointer at declaration time is called pointer initialization. • Example • Float temperature; • Float *Ptemperaturre = &temperature; • Or • Float temperature; • Float *Ptemperaturre; • Ptemperature = &temperature;
  • 19. POINTER INITIALIZATION • It should be considered that at the moment of declaring a pointer, the asterisk * indicates that it is a pointer variable and not the dereference operator.
  • 20. POINTER INITIALIZATION • Consider the following program to explain the concept of pointer initialization • Int main(){ • Float temperature; • Float *Ptemperature = &temperature; • Cout<<“the address of temperature is =“<<&temperature<<endl; • Cout<<“the value of *Ptemperature is = “<<Ptemperature<<endl; • Getch(); • Return 0; • }
  • 21. POINTER INITIALIZATION • Here the pointer Ptemperature is initialized with the address of the variable temperature. • Sometime we need to initialize a pointer to zero. Such pointers are called null pointers and they do not point to anything. Null pointer can be defined by assigning address 0 to them. • Consider the following initialization to demonstrate null pointer • Int *NPtr; // define null pointer • NPtr = 0; // this assigns address 0 to NPtr • the use of Null pointers is mostly done in dynamic memory allocation