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. 📝
<a name="understanding-pointers"></a>
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.
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>
Near pointers can access memory within the current segment (data segment). They are typically used for small variables.
A near pointer is declared by placing an asterisk (*) before the variable name.
int nearValue;
int *nearPointer;Here's an example of using near pointers:
#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>
Far pointers can access memory in the extra segment (heap). They are typically used for large variables or dynamically allocated memory.
A far pointer is declared by prefixing the variable name with far and placing an asterisk (*) before the variable name.
int far farValue;
int far *farPointer;Here's an example of using far pointers:
#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>
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.
A huge pointer is declared by prefixing the variable name with huge and placing an asterisk (*) before the variable name.
int huge hugeValue;
int huge *hugePointer;Here's an example of using huge pointers:
#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;
}Which type of pointers can access memory within the current segment (data segment)?
How is a far pointer declared in C?
What is the difference between near, far, and huge pointers in C?