Table of contents
1.
Introduction
2.
What is a Pointer in C?
3.
Syntax of C Pointers
4.
How to Use Pointers?
4.1.
Pointer Declaration
4.2.
Pointer Initialization
4.3.
Pointer Dereferencing
5.
C Pointer Example
5.1.
C
6.
Types of Pointers in C
6.1.
1. Null Pointers
6.2.
C
6.3.
2. Void Pointers
6.4.
C
6.5.
3. Pointer to Pointer
6.6.
C
6.7.
4. Function Pointers
6.8.
C
6.9.
5. Constant Pointers
6.10.
C
6.11.
6. Pointer to Constant Data
6.12.
C
6.13.
7. Constant Pointer to Constant Data
6.14.
C
6.15.
8. Integer Pointers
6.16.
C
6.17.
9. Array Pointers
6.18.
C
6.19.
10. Structure Pointers
6.20.
C
6.21.
11. Double Pointers
6.22.
C
7.
Other Types of Pointers in C
7.1.
1. Far Pointer
7.2.
2. Dangling Pointer
7.3.
3. Huge Pointer
7.4.
4. Near Pointer
7.5.
5. Complex Pointer
7.6.
6. Normalized Pointer
7.7.
7. File Pointer
8.
Size of Pointers in C
8.1.
How to Find the Size of Pointers in C?
8.2.
C
9.
C Pointer Arithmetic
9.1.
C
10.
C Pointers and Arrays
10.1.
C
11.
Uses of Pointers in C
12.
Advantages of Pointers
13.
Disadvantages of Pointers
14.
Frequently Asked Questions
14.1.
What is the purpose of pointers in C?
14.2.
How do you initialize a pointer in C?
14.3.
What is pointer arithmetic?
15.
Conclusion
Last Updated: Dec 20, 2024
Medium

Types of Pointers in C

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pointers are a key concept in C programming that enable efficient memory management and data manipulation by storing and accessing memory addresses directly.

Types of Pointers in C

This article will introduce pointers in C, covering their syntax, usage, different types, and their advantages and disadvantages. By the end of this article, you'll have a clear understanding of pointers, how to use them, and their role in C programming.

What is a Pointer in C?

In C programming, a pointer is a variable that holds the memory address of another variable. Instead of storing data directly, the pointer refers to the memory location where the data is kept, allowing for more flexible and efficient data management.

Example:

int num = 10;
int *ptr = #


Here, ptr is a pointer that holds the address of the variable num.

Syntax of C Pointers

The syntax for declaring a pointer involves using the asterisk (*) symbol. Here’s the basic syntax:

type *pointerName;
  • type is the data type of the variable the pointer points to.
     
  • pointerName is the name of the pointer.
     

Example:

int *p; // Pointer to an integer
char *c; // Pointer to a character

How to Use Pointers?

Pointers are used to:

  • Access variables indirectly: They allow you to modify the value of a variable without directly referencing it.
     
  • Dynamic memory allocation: Pointers are crucial for allocating and managing memory during runtime.
     
  • Function arguments: Pointers enable efficient passing of large data structures to functions.

Pointer Declaration

To declare a pointer, you need to specify the type of data the pointer will point to.

Example:

int *intPtr; // Pointer to an integer
float *floatPtr; // Pointer to a float

Pointer Initialization

Pointers need to be initialized to a valid memory address before use. You can do this using the address-of operator (&) or by assigning them the address of dynamically allocated memory.

Example:

int num = 5;
int *ptr = # // ptr now holds the address of num

Pointer Dereferencing

Dereferencing a pointer means accessing the value at the memory address stored in the pointer. This is done using the asterisk (*) operator.

Example:

int num = 20;
int *ptr = #
printf("%d", *ptr); // Output: 20


Here, *ptr gives the value stored at the address held by ptr.

C Pointer Example

Let’s look at a complete example of pointers in C:

  • C

C

#include <stdio.h>

int main() {

   int num = 10;

   int *ptr = &num; // Pointer initialization

   printf("Value of num: %d\n", num);

   printf("Address of num: %p\n", (void *)ptr);

   printf("Value of num through pointer: %d\n", *ptr);

   *ptr = 20; // Changing the value of num through the pointer

   printf("New value of num: %d\n", num);

   return 0;

}
You can also try this code with Online C Compiler
Run Code


Output:

Value of num: 10
Address of num: 0x7ffee0b4c7c4
Value of num through pointer: 10
New value of num: 20

Types of Pointers in C

Pointers in C can be categorized into various types:

1. Null Pointers

A null pointer is a pointer that does not point to any valid memory location. It indicates that the pointer is not currently associated with any object or variable. In C, you can initialize a pointer to NULL to show that it does not point to a valid address.

Example:

  • C

C

#include <stdio.h>

int main() {
int *ptr = NULL; // Null pointer initialization

if (ptr == NULL) {
printf("Pointer is null.\n");
}

return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Pointer is null.


Explanation: In this example, ptr is initialized to NULL, and the program checks if ptr is null.

2. Void Pointers

A void pointer is a pointer that can hold the address of any data type. It is a generic pointer and cannot be directly dereferenced. To use a void pointer, you need to cast it to the appropriate type.

Example:

  • C

C

#include <stdio.h>
int main() {
   int num = 10;
   void *ptr = &num; // Void pointer initialization

   printf("Value: %d\n", *(int *)ptr); // Casting and dereferencing

   return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Value: 10

 

Explanation: The void pointer ptr holds the address of num. To access the value, it is cast to an int * and then dereferenced.

3. Pointer to Pointer

A pointer to pointer is a pointer that holds the address of another pointer. This is useful for dynamic memory allocation and for working with multi-dimensional arrays.

Example:

  • C

C

#include <stdio.h>
int main() {
   int num = 5;
   int *ptr = &num;
   int **pptr = &ptr; // Pointer to pointer initialization

   printf("Value: %d\n", **pptr); // Dereferencing twice

   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Value: 5


Explanation: pptr is a pointer to ptr, which is a pointer to num. Dereferencing pptr twice gives the value of num.

4. Function Pointers

A function pointer is a pointer that points to a function instead of a data variable. This allows for dynamic function calls and can be used to implement callback functions.

Example

  • C

C

#include <stdio.h>

void printMessage(char *message) {
printf("%s\n", message);
}

int main() {
void (*funcPtr)(char *) = printMessage; // Function pointer initialization

funcPtr("Hello, Function Pointers!"); // Calling function via pointer

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Hello, Function Pointers!


Explanation: funcPtr is a function pointer that points to the printMessage function. It is used to call the function with the argument "Hello, Function Pointers!".

5. Constant Pointers

A constant pointer is a pointer whose address cannot be changed after initialization. The pointer itself is constant, but the data it points to can be modified if it is not a const pointer.

Example:

  • C

C

#include <stdio.h>
int main() {
   int num = 10;
   int *const ptr = &num; // Constant pointer

   *ptr = 20; // Allowed
   // ptr = NULL; // Error: cannot change address
   printf("Value: %d\n", *ptr);
   return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Value: 20


Explanation: ptr is a constant pointer to num. While the value of num can be modified, the pointer ptr cannot be reassigned to a different address.

6. Pointer to Constant Data

A pointer to constant data is a pointer that points to a value that cannot be modified through the pointer. The pointer itself can be changed to point to different addresses.

Example:

  • C

C

#include <stdio.h>
int main() {
   int num = 10;
   const int *ptr = &num; // Pointer to constant data
   // *ptr = 20; // Error: cannot modify value through pointer
   printf("Value: %d\n", *ptr);
   return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Value: 20


Explanation: ptr is a pointer to a constant integer. Although the pointer can point to different integers, the value of the integer it points to cannot be modified through ptr.

7. Constant Pointer to Constant Data

A constant pointer to constant data is a pointer that cannot change the address it points to, nor can it modify the value at that address. This is the most restrictive form of pointer.

Example:

  • C

C

#include <stdio.h>

int main() {
int num = 10;
const int *const ptr = &num; // Constant pointer to constant data

// *ptr = 20; // Error: cannot modify value
// ptr = NULL; // Error: cannot change address

printf("Value: %d\n", *ptr);

return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Value: 10


Explanation: ptr is a constant pointer to constant data. Neither the pointer address nor the value at that address can be changed.

8. Integer Pointers

An integer pointer is a pointer that specifically points to an integer data type. It is used to access and manipulate integer variables.

Example:

  • C

C

#include <stdio.h>

int main() {
int num = 25;
int *ptr = &num; // Integer pointer

printf("Value: %d\n", *ptr); // Dereferencing integer pointer

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Value: 25

 

Explanation: ptr is an integer pointer that holds the address of num. Dereferencing ptr gives the value of num.

9. Array Pointers

An array pointer is a pointer that points to the first element of an array. Arrays and pointers are closely related in C, and an array name can be treated as a pointer to its first element.

Example:

  • C

C

#include <stdio.h>

int main() {
   int arr[] = {10, 20, 30, 40};
   int *ptr = arr; // Array pointer

   for (int i = 0; i < 4; i++) {
       printf("%d ", *(ptr + i)); // Access array elements using pointer
   }
   printf("\n");

   return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

10 20 30 40 


Explanation: ptr is a pointer to the first element of the array arr. Pointer arithmetic is used to access each element of the array.

10. Structure Pointers

A structure pointer is a pointer that points to a structure. It is used to access and manipulate the members of a structure.

Example:

  • C

C

#include <stdio.h>

typedef struct {
int id;
char name[50];
} Student;

int main() {
Student stu = {1, "Alice"};
Student *ptr = &stu; // Structure pointer

printf("ID: %d\n", ptr->id);
printf("Name: %s\n", ptr->name);

return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

ID: 1
Name: Alice


Explanation: ptr is a pointer to the Student structure. The -> operator is used to access structure members through the pointer.

11. Double Pointers

A double pointer is a pointer that points to another pointer. It is commonly used in dynamic memory allocation, multi-dimensional arrays, and managing arrays of pointers.

Example:

  • C

C

#include <stdio.h>
#include <stdlib.h>

int main() {
int **arr;
int rows = 2;
int cols = 3;

// Allocating memory for array of pointers
arr = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int));
}

// Assigning values
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = i + j;
}
}

// Printing values
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

// Freeing allocated memory
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

0 1 2 
1 2 3 


Explanation: arr is a double pointer used to allocate and manage a 2D array dynamically. Memory is allocated for both the rows and columns, and the array is populated and printed.

Other Types of Pointers in C

In C programming, pointers are variables that store memory addresses, and they come in several types, each serving a unique purpose. Here’s an overview of various types of pointers:

1. Far Pointer

Far pointers are used in older 16-bit systems to access memory beyond the 64KB segment limit. They consist of a segment address and an offset address, allowing access to a larger memory space. Far pointers are specific to systems like DOS, which use segmented memory architecture.

2. Dangling Pointer

dangling pointer occurs when a pointer references a memory location after the memory has been deallocated or freed. This can lead to undefined behavior, such as crashes or data corruption. Dangling pointers can be avoided by setting the pointer to NULL after freeing the associated memory.

3. Huge Pointer

Huge pointers are similar to far pointers but are designed to handle memory segmentation and addressing in a more flexible way in 16-bit systems. They allow access to a larger address space by adjusting both segment and offset, overcoming the limitations of segment-based memory addressing.

4. Near Pointer

Near pointers are used in 16-bit systems to address memory within a single 64KB segment. They are limited to a specific segment and cannot access memory outside that segment. Near pointers are commonly used in environments where segmentation is a factor.

5. Complex Pointer

Complex pointers refer to pointers that involve multiple levels of indirection. For example, a pointer to a pointer or a pointer to a complex data structure like arrays of structures. These pointers are useful for advanced programming scenarios involving dynamic memory allocation and multi-dimensional data.

6. Normalized Pointer

Normalized pointers are adjusted to a standard or base address. They are often used in systems programming or hardware access to ensure that pointers are aligned or referenced to a specific base address. This alignment helps in maintaining consistent memory access and manipulation.

7. File Pointer

A file pointer in C is a pointer of type FILE * used to manage file operations. It is utilized with functions from the standard I/O library to handle various file operations, such as opening, reading, writing, and closing files.

Size of Pointers in C

The size of a pointer depends on the architecture of the machine (e.g., 32-bit or 64-bit). Typically, pointers are 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.

How to Find the Size of Pointers in C?

You can use the sizeof operator to find the size of a pointer.

Example:

  • C

C

#include <stdio.h>



int main() {

   int *ptr;

   printf("Size of pointer: %zu\n", sizeof(ptr));

   return 0;

}
You can also try this code with Online C Compiler
Run Code

 

Output (on a 64-bit system):

Size of pointer: 8

C Pointer Arithmetic

Pointer arithmetic involves operations like addition and subtraction to traverse arrays or memory blocks. Adding an integer to a pointer increases its address by that integer times the size of the data type it points to.

Example:

  • C

C

#include <stdio.h>



int main() {

   int arr[3] = {10, 20, 30};

   int *ptr = arr;



   printf("First element: %d\n", *ptr);

   ptr++;

   printf("Second element: %d\n", *ptr);

   return 0;

}
You can also try this code with Online C Compiler
Run Code

 

Output:

First element: 10
Second element: 20

C Pointers and Arrays

Pointers and arrays are closely related in C. An array name is essentially a pointer to its first element.

Example:

  • C

C

#include <stdio.h>

int main() {

   int arr[] = {1, 2, 3};

   int *ptr = arr;

   for (int i = 0; i < 3; i++) {

       printf("%d ", *(ptr + i));

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code


Output:

1 2 3

Uses of Pointers in C

  • Dynamic Memory Allocation: Pointers are crucial for allocating and deallocating memory at runtime, which is useful when the required amount of memory is not known until the program is running.
     
  • Function Arguments: Pointers enable you to pass large data structures or arrays to functions without copying the entire structure. This approach conserves memory and improves performance.
     
  • Implementing Data Structures: Pointers facilitate the creation of dynamic data structures like linked lists, stacks, queues, and trees, allowing efficient insertion and deletion of elements.
     
  • Pointer Arithmetic: Pointer arithmetic involves operations such as addition and subtraction on pointers, which is helpful for navigating through arrays and memory blocks.

Advantages of Pointers

  • Function Efficiency: Pointers make function argument passing more efficient by allowing you to pass large data structures by reference rather than by value.
     
  • Dynamic Memory Management: Pointers enable dynamic memory allocation and deallocation, allowing programs to manage memory more flexibly and efficiently. This is essential for handling varying data sizes and optimizing resource usage.
     
  • Efficient Array Handling: Pointers can be used to manipulate arrays more effectively. By using pointer arithmetic, you can traverse and modify array elements without the overhead of array indexing.

Disadvantages of Pointers

  • Complexity: Pointers can be challenging to understand and manage, especially for beginners, due to their abstract nature.
     
  • Memory Leaks: Improper handling of pointers can lead to memory leaks, where allocated memory is not properly deallocated.
     
  • Security Risks: Uninitialized or invalid pointers can cause undefined behavior and security vulnerabilities, making programs prone to crashes or exploits.

Frequently Asked Questions

What is the purpose of pointers in C?

Pointers are used to store memory addresses and provide indirect access to variables, facilitating efficient memory management and data manipulation.

How do you initialize a pointer in C?

A pointer is initialized by assigning it the address of a variable using the address-of operator (&).

What is pointer arithmetic?

Pointer arithmetic involves performing operations like addition and subtraction on pointers to navigate through memory locations, which is often utilized with arrays.

Conclusion

In this article, we discussed the fundamentals of pointers in C, including their syntax, usage, and types. We covered how to declare, initialize, and use pointers, along with their advantages and disadvantages. Learning pointers is essential for effective programming in C, as they offer powerful capabilities for memory management and data manipulation.

Live masterclass