Pointers are variables that store the memory address of another variable rather than its immediate value. These variables can be of several types, including int, float, and char.
There are three steps to using pointers in C:
We don't initialize the pointer; instead, we declare it with the * (asterisk symbol) before the pointer name.
When we initialize a pointer, we use the & (address of) operator to specify the memory address of another variable.
Dereferencing a pointer allows you to access the value stored at the memory address specified by the pointer. The (*) dereferencing operator is used exactly as it was in the pointer declaration.
Following are the types of pointers in C
Pointers that store memory addresses for integer variables, allowing them to be manipulated and referenced via pointer arithmetic.
Arrays and pointers have a close connection between concepts. The array name itself serves as a pointer to the first element. They are also referred to as pointers to arrays.
The pointer referring to the structure type is known as the Structure Pointer or Pointer to Structure. It can be declared in the same manner as the other primitive data types.
Function pointers direct users to specific functions. They differ from other pointers in that they point to code rather than data.
In the C programming language, we can define a pointer that holds the memory address of another pointer. These pointers are known as double-pointers or pointer-to-pointers. Instead of referring to a specific data value, they refer to another pointer.
Null Pointers are pointers that do not refer to any memory location. They can be created by passing a NULL value to the pointer. NULL can be assigned to any type of pointer.
In C, void pointers are pointers of the type void. It signifies they have no related data type. They are also known as generic pointers since they can refer to any type and be typecasted to any type.
Pointers that are not yet initialized with anything are known as wild pointers. These C-pointers can cause problems in our programs, eventually causing them to crash. If values are modified via wild pointers, they may result in data abort or corruption.
Constant pointers store a constant memory address that cannot be changed once defined. It will consistently point to the same memory address.
Pointers to a constant are those that point to an immutable constant value. We can only view the data pointed to by the pointer, not modify it. However, we can set the address held in the pointer to a constant.
In addition to the ones mentioned above, there are other types of pointers accessible in C:
A far pointer is often a 32-bit value that can reach memory beyond the current segment.
Dangling pointers are those that point to a memory block that has been deallocated. This causes an error called the Dangling Pointer Problem. This happens when a pointer to a variable is out of scope or an object is deallocated. There are two methods to prevent dangling pointer issues:
A 32-bit pointer that contains the segment and offset addresses.
Complex pointers are pointers that have numerous levels of indirection.
A near pointer is a 16-bit address that is stored within the current segment of a 16-bit computer.
This is a 32-bit pointer that stores as much of its value in the segment register as possible.
The pointer to a FILE data type is referred to as a stream pointer or a file pointer.
In C, all pointer types have the same size. The pointer's size is independent of the type to which it points. It simply depends on the operating system and CPU architecture. Pointers in C have the following sizes: