SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
Types of Pointers

TYPES OF POINTERS
Void Pointer:
It is a special type of pointer that can be pointed at objectsof any data type.A void pointer is
declared like a normal pointer,using the void keyword as the pointer’s type.
Example :
float *f;
int i;
f = &i;

//pointer of type float
//integer variable
//compilation error

The above problem can be solved by general purpose pointer called void pointer.
Void pointer can be declared as follows:
void *v // defines a pointer of type void
Wild Pointer:
A pointer in c which has not been initialized is known as wild pointer.
Example :
What will be output of following c program?
#include<stdio.h>
int main(){
int *ptr;
printf("%un",ptr);
printf("%d",*ptr);
return 0;
}
Output: Any address
Garbage value
Here ptr is wild pointer because it has not been initialized.
There is difference between the NULL pointer and wild pointer.
Null pointer points the base address of segment
wild pointer doesn’t point any specific memory location.

Created By: Ravindra

Page 1
Types of Pointers

Different types of pointers:
1] Dangling Pointer:
If any pointer is pointing the memory address of any variable but after some variable has
deleted from that memory location while pointer is still pointing such memory location. Such
pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Example :
#include<stdio.h>
#include<stdlib.h>
main()
{
int *p,n,i;
p=(int *)malloc(n*sizeof(int));
printf("Before FREE = %d",p);
free(p);
p=NULL;
printf("After FREE = %d",p);
return 0;
}
It will remove the problem of Dangling Pointers, but if you tries to access the *ptr it goes to
general protection, leading to arise the dangling pointers problem.
In TURBO C there are three types of pointers. TURBO C works under DOS operating system
which is based on 8085 microprocessor.
1. Near pointer
2.Far pointer
3.Huge pointer
1. Near pointer:
The pointer which can points only 64KB data segment or segment number 8 is known as near
pointer.
That is near pointer cannot access beyond the data segment like graphics video memory, text
video memory etc. Size of near pointer is two byte. With help keyword near, we can make any
pointer as near pointer.
Example :
(1)
#include<stdio.h>
int main(){
int x=25;
Created By: Ravindra

Page 2
Types of Pointers

int near* ptr;
ptr=&x;
printf(“%d”,sizeof ptr);
return 0;
}
Output: 2
Explanation: Size of any type of near pointer is two byte.
2. Far pointer:
The pointer which can point or access whole the residence memory of RAM i.e. which can
access all 16 segments is known as far pointer.
Size of far pointer is 4 byte or 32 bit.
Example :
#include<stdio.h>
int main(){
int far *near*ptr;
printf("%d %d",sizeof(ptr) ,sizeof(*ptr));
return 0;
}
Output: 4 2
Explanation: ptr is far pointer while *ptr is near pointer.
Example :
#include<stdio.h>
int main(){
int far *p,far *q;
printf("%d %d",sizeof(p) ,sizeof(q));
return 0;
}
Output: 4 4
First 16 bit stores: Segment number
Next 16 bit stores: Offset address
Now here question arises what is segment number & Offset address, Let’s check it…
Example :
#include<stdio.h>
int main(){
int x=100;
int far *ptr;
Created By: Ravindra

Page 3
Types of Pointers

ptr=&x;
printf("%Fp",ptr);
return 0;
}
Output: 8FD8:FFF4.
Here 8FD8 is segment address and FFF4 is offset address in hexadecimal number format.
Note: %Fp is used for print offset and segment address of pointer in printf function in
hexadecimal number format.
# Limitation of far pointer:
We cannot change or modify the segment address of given far address by applying any
arithmetic operation on it. That is by using arithmetic operator we cannot jump from one
segment to other segment. If you will increment the far address beyond the maximum value of its
offset address instead of incrementing segment address it will repeat its offset address in cyclic
order.

3. Huge Pointer:
The pointer which can point or access whole the residence memory of RAM i.e. which can
access all the 16 segments is known as huge pointer.
Size of huge pointer is 4 byte or 32 bit.
Example :
#include<stdio.h>
int main(){
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
return 0;
}
Output: 4 4 1
Explanation: p is huge pointer, *p is far pointer and **p is char type data variable.

Created By: Ravindra

Page 4

Mais conteúdo relacionado

Mais procurados (20)

Functions in c
Functions in cFunctions in c
Functions in c
 
C tokens
C tokensC tokens
C tokens
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
C functions
C functionsC functions
C functions
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Strings
StringsStrings
Strings
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Semelhante a Types of pointer in C

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptxvijayapraba1
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfKosmikTech1
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in cRahul Budholiya
 

Semelhante a Types of pointer in C (20)

C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
 

Último

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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Último (20)

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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Types of pointer in C

  • 1. Types of Pointers TYPES OF POINTERS Void Pointer: It is a special type of pointer that can be pointed at objectsof any data type.A void pointer is declared like a normal pointer,using the void keyword as the pointer’s type. Example : float *f; int i; f = &i; //pointer of type float //integer variable //compilation error The above problem can be solved by general purpose pointer called void pointer. Void pointer can be declared as follows: void *v // defines a pointer of type void Wild Pointer: A pointer in c which has not been initialized is known as wild pointer. Example : What will be output of following c program? #include<stdio.h> int main(){ int *ptr; printf("%un",ptr); printf("%d",*ptr); return 0; } Output: Any address Garbage value Here ptr is wild pointer because it has not been initialized. There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment wild pointer doesn’t point any specific memory location. Created By: Ravindra Page 1
  • 2. Types of Pointers Different types of pointers: 1] Dangling Pointer: If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem. Example : #include<stdio.h> #include<stdlib.h> main() { int *p,n,i; p=(int *)malloc(n*sizeof(int)); printf("Before FREE = %d",p); free(p); p=NULL; printf("After FREE = %d",p); return 0; } It will remove the problem of Dangling Pointers, but if you tries to access the *ptr it goes to general protection, leading to arise the dangling pointers problem. In TURBO C there are three types of pointers. TURBO C works under DOS operating system which is based on 8085 microprocessor. 1. Near pointer 2.Far pointer 3.Huge pointer 1. Near pointer: The pointer which can points only 64KB data segment or segment number 8 is known as near pointer. That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. Size of near pointer is two byte. With help keyword near, we can make any pointer as near pointer. Example : (1) #include<stdio.h> int main(){ int x=25; Created By: Ravindra Page 2
  • 3. Types of Pointers int near* ptr; ptr=&x; printf(“%d”,sizeof ptr); return 0; } Output: 2 Explanation: Size of any type of near pointer is two byte. 2. Far pointer: The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer. Size of far pointer is 4 byte or 32 bit. Example : #include<stdio.h> int main(){ int far *near*ptr; printf("%d %d",sizeof(ptr) ,sizeof(*ptr)); return 0; } Output: 4 2 Explanation: ptr is far pointer while *ptr is near pointer. Example : #include<stdio.h> int main(){ int far *p,far *q; printf("%d %d",sizeof(p) ,sizeof(q)); return 0; } Output: 4 4 First 16 bit stores: Segment number Next 16 bit stores: Offset address Now here question arises what is segment number & Offset address, Let’s check it… Example : #include<stdio.h> int main(){ int x=100; int far *ptr; Created By: Ravindra Page 3
  • 4. Types of Pointers ptr=&x; printf("%Fp",ptr); return 0; } Output: 8FD8:FFF4. Here 8FD8 is segment address and FFF4 is offset address in hexadecimal number format. Note: %Fp is used for print offset and segment address of pointer in printf function in hexadecimal number format. # Limitation of far pointer: We cannot change or modify the segment address of given far address by applying any arithmetic operation on it. That is by using arithmetic operator we cannot jump from one segment to other segment. If you will increment the far address beyond the maximum value of its offset address instead of incrementing segment address it will repeat its offset address in cyclic order. 3. Huge Pointer: The pointer which can point or access whole the residence memory of RAM i.e. which can access all the 16 segments is known as huge pointer. Size of huge pointer is 4 byte or 32 bit. Example : #include<stdio.h> int main(){ char huge * far *p; printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p)); return 0; } Output: 4 4 1 Explanation: p is huge pointer, *p is far pointer and **p is char type data variable. Created By: Ravindra Page 4