Types of pointer in C

501
-1

Published on

Types of pointer

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
501
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Types of pointer in C

  1. 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. 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. 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. 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

×