C Pointers
C Pointers
Pointers are one of the core components of the C programming language. A pointer can be used to
store the memory address of other variables, functions, or even other pointers. The use of pointers
allows low-level memory access, dynamic memory allocation, and many other functionality in C.
Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.
datatype * ptr;
where
The above syntax is used to define a pointer to a variable. We can also define pointers to
functions, structures, etc
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer,
we use the ( * ) dereference operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it is not initialized. Such
pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We
generally use the ( & ) addressof operator to get the memory address of a variable and then
store it in the pointer variable.
Example
We can also declare and initialize the pointer in a single step. This method is called pointer
definition as the pointer is declared and initialized at the same time.
Example
Note: It is recommended that the pointers should always be initialized to some value before
starting using it. Otherwise, it may lead to number of errors.
3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address
specified in the pointer. We use the same ( * ) dereferencing operator that we used in the
pointer declaration.
Dereferencing a Pointer in C
C Pointer Example
// C program to illustrate Pointers
#include <stdio.h>
void geeks()
int* ptr;
ptr = &var;
// Driver program
int main()
geeks();
return 0;
Output
Pointers in C can be classified into many different types based on the parameter on which we are
defining their types. If we consider the type of variable stored in the memory location pointed by
the pointer, then the pointers can be classified into the following types:
1. Integer Pointers
As the name suggests, these are the pointers that point to the integer values.
Syntax
int *ptr;
Similarly, a pointer can point to any primitive data type. It can point also point to derived data
types such as arrays and user-defined data types such as structures.
2. Array Pointer
Pointers and Array are closely related to each other. Even the array name is the pointer to its first
element. They are also known as Pointer to Arrays. We can create a pointer to an array using the
given syntax.
Syntax
Pointer to Arrays exhibits some interesting properties which we discussed later in this article.
3. Structure Pointer
The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. It can
be declared in the same way as we declare the other primitive data types.
Syntax
In C, structure pointers are used in data structures such as linked lists, trees, etc.
4. Function Pointers
Function pointers point to the functions. They are different from the rest of the pointers in the
sense that instead of pointing to the data, they point to the code. Let’s consider a function
prototype – int func (int, char), the function pointer for this function will be
Syntax
Note: The syntax of the function pointers changes according to the function prototype.
5. Double Pointers
In C language, we can define a pointer that stores the memory address of another pointer. Such
pointers are called double-pointers or pointers-to-pointer. Instead of pointing to a data value,
they point to another pointer.
Syntax
datatype ** pointer_name;
Note: In C, we can create multi-level pointers with any number of levels such as – ***ptr3,
****ptr4, ******ptr5 and so on.
6. NULL Pointer
The Null Pointers are those pointers that do not point to any memory location. They can be
created by assigning a NULL value to the pointer. A pointer of any type can be assigned the
NULL value.
Syntax
It is said to be good practice to assign NULL to the pointers currently not in use.
7. Void Pointer
The Void pointers in C are the pointers of type void. It means that they do not have any
associated data type. They are also called generic pointers as they can point to any type and can
be typecasted to any type.
Syntax
void * pointer_name;
One of the main properties of void pointers is that they cannot be dereferenced.
8. Wild Pointers
The Wild Pointers are pointers that have not been initialized with something yet. These types of
C-pointers can cause problems in our programs and can eventually cause them to crash.
Example
int *ptr;
char *str;
9. Constant Pointers
In constant pointers, the memory address stored inside the pointer is constant and cannot be
modified once it is defined. It will always point to the same memory address.
Syntax
The pointers pointing to a constant value that cannot be modified are called pointers to a
constant. Here we can only access the data pointed by the pointer, but cannot modify it.
Although, we can change the address stored in the pointer to constant.
Syntax
Disadvantages of Pointers
In C programming language, pointers and arrays are closely related. An array name acts like a
pointer constant. The value of this pointer constant is the address of the first element. For
example, if we have an array named val then val and &val[0] can be used interchangeably.
If we assign this value to a non-constant pointer of the same type, then we can access the
elements of the array using this pointer.
#include <stdio.h>
void geeks()
// Declare an array
int val[3] = { 5, 10, 15 };
int* ptr;
ptr = val;
return;
// Driver program
int main()
geeks();
return 0;
Output
Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather
they are linked using pointers. Linked List forms a series of connected nodes, where each node stores
the data and the address of the next node.
Dynamic Size: Linked lists can grow or shrink dynamically, as memory allocation is done at
runtime.
Insertion and Deletion: Adding or removing elements from a linked list is efficient, especially for
large lists.
Flexibility: Linked lists can be easily reorganized and modified without requiring a contiguous
block of memory.
Random Access: Unlike arrays, linked lists do not allow direct access to elements by index.
Traversal is required to reach a specific node.
Extra Memory: Linked lists require additional memory for storing the pointers, compared to
arrays.