C Near, Far, Huge Pointers (DOS) 🎯

beginner
10 min

C Near, Far, Huge Pointers (DOS) 🎯

Welcome to a comprehensive guide on C Near, Far, and Huge Pointers! This lesson is designed for both beginners and intermediates, so let's dive in. 📝

Table of Contents

  1. Understanding Pointers 1.1. What is a Pointer? 1.2. Why use Pointers?
  2. Near Pointers 2.1. Declaring Near Pointers 2.2. Example: Using Near Pointers
  3. Far Pointers 3.1. Declaring Far Pointers 3.2. Example: Using Far Pointers
  4. Huge Pointers 4.1. Declaring Huge Pointers 4.2. Example: Using Huge Pointers

<a name="understanding-pointers"></a>

1. Understanding Pointers 📝

1.1. What is a Pointer? 💡

In C programming, a pointer is a variable that stores the memory address of another variable. It allows you to access and manipulate memory locations directly.

1.2. Why use Pointers? 💡

Pointers are essential for functions that require dynamic memory allocation, working with arrays, linked lists, and accessing memory locations that are not consecutive.

<a name="near-pointers"></a>

2. Near Pointers 💡

Near pointers can access memory within the current segment (data segment). They are typically used for small variables.

2.1. Declaring Near Pointers 💡

A near pointer is declared by placing an asterisk (*) before the variable name.

c
int nearValue; int *nearPointer;

2.2. Example: Using Near Pointers 💡

Here's an example of using near pointers:

c
#include <stdio.h> int main() { int nearValue = 42; int *nearPointer; // Assign the address of nearValue to nearPointer nearPointer = &nearValue; printf("The value of nearValue is: %d\n", nearValue); printf("The memory address of nearValue is: %p\n", &nearValue); printf("The value stored in nearPointer is: %p\n", nearPointer); printf("The value stored in nearValue through nearPointer is: %d\n", *nearPointer); return 0; }

<a name="far-pointers"></a>

3. Far Pointers 💡

Far pointers can access memory in the extra segment (heap). They are typically used for large variables or dynamically allocated memory.

3.1. Declaring Far Pointers 💡

A far pointer is declared by prefixing the variable name with far and placing an asterisk (*) before the variable name.

c
int far farValue; int far *farPointer;

3.2. Example: Using Far Pointers 💡

Here's an example of using far pointers:

c
#include <stdio.h> #include <alloc.h> int main() { int far farValue = 100; int far *farPointer; // Allocate memory for a far variable farPointer = far_malloc(sizeof(int)); printf("The value of farValue is: %d\n", farValue); printf("The memory address of farValue is: %p\n", &farValue); // Assign the memory address of farPointer to farValue *farPointer = farValue; printf("The value stored in farPointer is: %p\n", farPointer); printf("The value stored in farValue through farPointer is: %d\n", *farPointer); // Allocate memory for another far variable and store the value far_malloc((far)&farValue + sizeof(int)); *(farPointer + 1) = 200; printf("The value stored in farPointer[1] is: %d\n", *(farPointer + 1)); return 0; }

<a name="huge-pointers"></a>

4. Huge Pointers 💡

Huge pointers can access memory in the huge segment (extended memory). They are typically used for very large variables or dynamically allocated memory beyond the heap.

4.1. Declaring Huge Pointers 💡

A huge pointer is declared by prefixing the variable name with huge and placing an asterisk (*) before the variable name.

c
int huge hugeValue; int huge *hugePointer;

4.2. Example: Using Huge Pointers 💡

Here's an example of using huge pointers:

c
#include <stdio.h> #include <alloc.h> int main() { int huge hugeValue = 400; int huge *hugePointer; // Allocate memory for a huge variable hugePointer = huge_malloc(sizeof(int)); printf("The value of hugeValue is: %d\n", hugeValue); printf("The memory address of hugeValue is: %p\n", &hugeValue); // Assign the memory address of hugePointer to hugeValue *hugePointer = hugeValue; printf("The value stored in hugePointer is: %p\n", hugePointer); printf("The value stored in hugeValue through hugePointer is: %d\n", *hugePointer); // Allocate memory for another huge variable and store the value huge_malloc((huge)&hugeValue + sizeof(int)); *(hugePointer + 1) = 500; printf("The value stored in hugePointer[1] is: %d\n", *(hugePointer + 1)); return 0; }
Quick Quiz
Question 1 of 1

Which type of pointers can access memory within the current segment (data segment)?

Quick Quiz
Question 1 of 1

How is a far pointer declared in C?

Quick Quiz
Question 1 of 1

What is the difference between near, far, and huge pointers in C?