What is a Pointer




What is a Pointer?

computers

A pointer is a programming language data type where the value directly points to another value stored somewhere else in the computer memory using its address. In high level programming languages, pointers effectively take the place of general purpose registers in low level languages such as assembly language or machine code, but may be in available memory. A pointer references a specific location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing a pointer. A pointer is a simple, less abstracted implementation of the more abstracted reference data type (although it is not as directly usable as a C++ reference). Several languages support some type of pointer, although some languages are more restricted than others.

Pointers to data significantly improve performance for repetitive operations such as traversing strings, lookup tables, control tables and tree structures. It is typically takes less time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point to. Pointers are also used to store the addresses of entry points for called subroutines in procedural/imperative programming and for run-time linking to dynamic link libraries (DLLs). In Object-oriented programming (OOP), pointers to functions are used for binding methods, often using what are called virtual method tables.

Although pointers have been used to refer to references in general, it more properly applies to data structures whose interface explicitly allows the pointer to be manipulated (arithmetically via pointer arithmetic) as a memory address, as opposed to a magic cookie or capability where this is not possible. Pointers allow both protected and unprotected access to memory addresses and therefore there are risks associated with using them particularly in the latter case.

Data structures such as linked lists and trees are required to have pointers to help manage how the structure is implemented and controlled. For example: start pointers, end pointers, and stack pointers may be required. These pointers can either be absolute (the actual physical address or a virtual address in virtual memory) or relative (an offset from an absolute start address that typically uses less bits than a full address, but will typically require one additional arithmetic operation to resolve). See example below:

A simple linked list data structure:


#include <stdio.h>
#include <stdlib.h>	// required for malloc

// a simple linked list data structure

typedef struct data
{
	int x;
	struct data *next;	
};

typedef struct data d;

int main(void)
{
	// create start and current (nav) pointers to know where to 
	// start and where you are in the lsit
	d *nav, *start;	
	int x;
	
	start=NULL;
	
	// fill list with 0 to 10
	for(x=0;x<11;x++)
	{
		// dynamically memory allocate (malloc) some new space for each number
		nav=(d*)malloc(sizeof(d));		
		nav->x=x;		// assign x from the loop to x in each node of the linked list
		
		nav->next=start;
		start=nav;
	}
	
	nav=start;
	
	// while you have not reached the end of the list print the next entry
	while(nav)
	{
		printf("%2d \n",nav->x);	
		nav=nav->next;		// move to next number in the list
	}
	
	return 0;
}

Output:

10
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0