SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Pointer in c
Pointer in cPointer in c
Pointer in c
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
File in C language
File in C languageFile in C language
File in C language
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Array in c++
Array in c++Array in c++
Array in c++
 

Similar a Types of pointer in C

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 

Similar a Types of pointer in C (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
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
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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.
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

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