Home >Backend Development >C++ >Explain Near, Far and Huge pointers in C language

Explain Near, Far and Huge pointers in C language

WBOY
WBOYforward
2023-09-14 15:13:021580browse

Explain Near, Far and Huge pointers in C language

Based on the memory model and segment, pointers are divided into three types -

  • Near pointer
  • Far pointer
  • Large pointer

Near pointer

  • is a pointer that works within the 64Kb memory data segment range.

  • It cannot access addresses beyond this data segment.

  • Near pointers can be incremented or decremented by using the address range arithmetic operators.

  • Using the keyword near, we can set any pointer to a near pointer.

Grammar

The syntax is as follows-

<data type> near <pointer definition>
<data type> near <function definition>

The following statement declares the near pointer of variable s

char near *string;

Program

The following program shows the usage of near pointers.

#include<stdio.h>
int main(){
   int number=50;
   int near* p;
   p=&number;
   printf("%d",sizeof(p));
   return 0;
}

Output

The output is as follows-

2

Far pointer

  • It is a pointer, which stores different pointers offset and segment address.

  • It has access to all 16 segments.

  • The far pointer address range is 0 to 1MB.

  • When the pointer is incremented or decremented, only the offset part changes.

Grammar

The syntax is as follows-

<data type> far <pointer definition>
<data type> far <function definition>

The following statement declares the far pointer of variable s

char far *s;

Program

The following program shows the usage of far pointers.

#include<stdio.h>
int main(){
   int number=50;
   int far *p;
   p=&number;
   printf("%d",sizeof number);
   return 0;
}

Output

The output is as follows -

4

Large pointer

  • It is a size similar to that of a far pointer Pointers, since both are 32-bit addresses.

  • Large pointers can be incremented without being affected by segment work loops.

Procedure

The following program shows the use of large pointers.

#include<stdio.h>
Int main(){
   Char huge *far *ptr;
   Printf("%d%d%d",sizeof(ptr),sizeof(*ptr),sizeof(**ptr));
   Return 0;
}

Output

The output is as follows -

4 4 1

The above is the detailed content of Explain Near, Far and Huge pointers in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete