Pointers in C

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What are pointers in C?

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.

How Do You Use a Pointer?

There are three steps to using pointers in C:

  1. Pointer Declaration in C
  2. Pointer Initialization in C
  3. Dereferencing Pointer in C

Pointer Declaration in C

We don't initialize the pointer; instead, we declare it with the * (asterisk symbol) before the pointer name.

Pointer Initialization in C

When we initialize a pointer, we use the & (address of) operator to specify the memory address of another variable.

Dereferencing pointers in C

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.

Different types of Pointers in C

Following are the types of pointers in C

  • Integer Pointers
  • Array Pointer
  • Structure Pointer
  • Function Pointers
  • Double Pointers
  • NULL Pointer
  • Void Pointer
  • Wild Pointers
  • Constant Pointers
  • Pointer to Constant

Integer Pointers

Pointers that store memory addresses for integer variables, allowing them to be manipulated and referenced via pointer arithmetic.

Array Pointer

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. 

Structure Pointer

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

Function pointers direct users to specific functions. They differ from other pointers in that they point to code rather than data.

Double Pointers

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 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.

Void 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.

Wild Pointers

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

Constant pointers store a constant memory address that cannot be changed once defined. It will consistently point to the same memory address.

Pointer to Constant

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.

Other Types of Pointers

In addition to the ones mentioned above, there are other types of pointers accessible in C:

  • Far pointer
  • Dangling pointer
  • Huge pointer
  • Complex pointer
  • Near pointer
  • Normalized pointer
  • File Pointer

Far pointer

A far pointer is often a 32-bit value that can reach memory beyond the current segment.

Dangling pointer

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:

  1. Assigning NULL to the pointer when memory is deallocated
  2. Use static variables

Huge pointer

A 32-bit pointer that contains the segment and offset addresses.

Complex pointer

Complex pointers are pointers that have numerous levels of indirection.

Near pointer

A near pointer is a 16-bit address that is stored within the current segment of a 16-bit computer.

Normalized pointer

This is a 32-bit pointer that stores as much of its value in the segment register as possible.

File Pointer

The pointer to a FILE data type is referred to as a stream pointer or a file pointer.

Size of pointers in C

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:

  • 8 bytes for a 64-bit system
  • Four bytes for a 32-bit system
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this