Pointers in C: Types of Pointers

Sakshi Dhameja  17 min read
12 Sep 2023
Advanced
648 Views

Pointers in C Programming: An Overview

Are you ready to embark on a journey of exploration and skill-building in C programming? If so, you've come to the right place! In this comprehensive C tutorial, we'll take you on a guided tour of the C language, from its foundational concepts to more advanced techniques.  In addition to the valuable insights shared in this article, consider taking advantage of C Online Training to enhance your knowledge and proficiency further.
We'll provide the information on pointers that will not only help build your knowledge in C programming but also increase your understanding of coding as a whole. Let's get into it!

What are Pointers in C?

C - Pointers are an essential and powerful aspect of the C programming language, often utilized by programmers to increase efficiency and flexibility in their code. At their core, pointers are variables that contain the memory address of another variable instead of a direct value.
By referencing this memory address, programmers can access and modify data indirectly, which allows for the dynamic allocation of memory in C as well as the ability to pass large data structures to functions without duplicating memory.
Furthermore, pointer in C programming enable users to create complex data structures, such as linked lists and trees, which are indispensable in various programming scenarios. The mastery of pointers is crucial for any C programmer to truly unleash the potential of this versatile language.

Use of pointers in C language

Pointer Declaration in C

We do not initialize the pointer; we simply declare it on pointer declaration. We use the (*) dereference operator before a pointer's name to declare it.

Syntax

int *a;//pointer to int 
char *c;//pointer to char 

Example

#include<stdio.h> 
int main(){ 
int number=120; 
int *p; 
p=&number;//stores the address of number variable 
printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number. 
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p. 
return 0; 
} 

This C code shows how to access and work with data in memory by declaring an integer variable number, a pointer p, & printing the address as well as the value of the number using pointer operations.

Output

Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 120

Pointer Initialization in C

The procedure of initializing a pointer is where we give the pointer variable a starting value. Typically, we obtain a variable's memory address using the (&) addressof operator and then save it in the pointer variable.

Example

#include <stdio.h>int main() {
    int x = 42; // Declare and initialize an integer variable 'x'
    int *ptr = &x; // Declare a pointer 'ptr' and initialize it with the address of 'x'
    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", &x);
    printf("Value pointed to by ptr: %d\n", *ptr);
    return 0;
}

In this C code, an integer variable named x is initialized to the value 42, a pointer named ptr is created to hold x's address in memory, and the values of x, its memory address, and ptr are printed. It shows how pointers allow for the indirect access and manipulation of data.

Read more about Variables in C Programming

Output

Value of x: 42
Address of x: 0x7ffeedf0 // The address will be different on your system
Value pointed to by ptr: 42

Dereferencing Pointer in C

Dereferencing a pointer involves gaining access to the value kept at the memory address that the pointer specifies. The (*) dereferencing operator is used just as it was in the pointer declaration.

Example

#include <stdio.h>int main() {
    int y = 99; // Declare and initialize an integer variable 'y' with a different value
    int *ptr = &y; // Declare a pointer 'ptr' and initialize it with the address of 'y'
    printf("Value pointed to by ptr: %d\n", *ptr); // Dereferencing 'ptr' to access and print the value of 'y'
    return 0;
}

This C code declares a pointer ptr, initializes an integer variable y to 99, and then uses dereferencing to output the value of y through ptr. It serves as an example of how pointers allow for memory-based indirect data access.

Output

Value pointed to by ptr: 99

Types of Pointers in C

Depending on the parameters we use to define their types, pointers can be divided into a wide variety of types. The following types of pointers can be distinguished based on the type of variable that is kept in the memory location that the pointer is pointing to:

Integer Pointers:

The memory location of an integer variable is stored in integer pointers.

Syntax

int* intPointer;

Array Pointer:

The initial element of an array may be referenced by array pointers.

Syntax

int arr[5];
int* arrPointer = arr;

Structure Pointer:

To point to a structure or a user-defined data type, use a structure pointer.

Syntax

struct MyStruct {
    int data;
};
struct MyStruct* structPointer;

Function Pointers:

You can call functions indirectly by using function pointers, which save the address of a function.

Syntax

int (*funcPointer)(int, int);

Double Pointers:

Double pointers are pointers to pointers and are frequently employed in linked data structures or multi-dimensional arrays.

Syntax

int doublePointer;

NULL Pointer:

A NULL pointer is used to signify that a pointer has not been initialized; it points to nowhere in memory.

Syntax

int* nullPointer = NULL;

Void Pointer:

In general, void pointers can point to any type of data.

Syntax

void* voidPointer;

Wild Pointers:

Wild pointers are dangling or uninitialized pointers that point to arbitrary locations in memory.

Syntax

int *ptr;
char *str;

Constant Pointers:

Constant pointers direct memory access to a fixed place.

Syntax

const int* constPointer;

Pointer to Constant:

A constant pointer points to data that the pointer cannot change.

Syntax

int const * pointerToConst;

Far Pointer:

Early versions of the x86 architecture used far pointers to access extended memory. Today's programming hardly ever makes use of them.

Syntax

char far *s; 

Dangling Pointer:

A dangling pointer is a pointer that frequently occurs after the memory it points to has been released or deallocated and points to an incorrect address in memory.

Syntax

int *ptr = malloc(sizeof(int)); // ptr points to a valid memory location
free(ptr); // ptr now points to a dangling memory location
{
int a = 10;
int *ptr = &a; // ptr points to a valid memory location
} // a goes out of scope, ptr now points to a dangling memory location

Huge Pointer:

In the early x86 architecture, huge pointers were utilized to access enormous memory models. Today's programming hardly ever makes use of them.

Syntax

int huge *p;

Complex Pointer:

Depending on the use case, complex pointers may refer to pointers having complex data structures that need a particular syntax.

Syntax

int **p;

Near Pointer:

Early versions of the x86 architecture employed near pointers to access tiny memory models. Today's programming hardly ever makes use of them.

Syntax

char near *string; 

Normalized Pointer

In low-level systems programming, normalized pointers are frequently used to guarantee that the address of the pointer is within the legal address space.

Syntax

int *ptr = _mkptr(0x12, 0x34);

File Pointer:

In file I/O operations, file pointers are used to keep track of the current location in a file.

Syntax

FILE* filePointer;

Size of Pointers in C

The C function sizeof the Pointer is important for figuring out how much system memory is being used. It is helpful since it might indicate how many bytes of memory are used up in the system. In C, we use the sizeof() operator to output the size of a pointer.

Syntax

sizeof(datatype/data)

Advantages of using Pointers in C

The following are the main advantages of using pointers in C:

  • Pointers are used in the dynamic deallocation and allocation of memory.
  • Pointers allow for effective access to an array or a structure.
  • For gaining access to memory regions, pointers are helpful.
  • Complex data structures like linked lists, graphs, trees, etc. are created using pointers.
  • Pointers shorten both the program's overall length and its execution time.

Disadvantages of Pointers in C

Pointers have the following disadvantages and are capable of errors:

  • If pointers are given the incorrect value, memory corruption may result.
  • It can be challenging to comprehend pointers.
  • In C, pointers are mostly to blame for memory leaks.
  • In C, pointers move more slowly than variables.
  • A segmentation fault might be brought on by uninitialized pointers.

Usage of Pointers in C language

  • Dynamic Memory Allocation: Pointers are used to allocate memory dynamically at run time using functions like malloc() and calloc(). This allows to creation of data structures such as arrays and linked lists of arbitrary size.
  • Structures: Usage of Pointers in C to manipulate structures, which are collections of variables of different types.
  • Passing Arguments to Functions: Pointers can be used to pass arguments to functions by reference, allowing the function to modify the values of variables passed to it.
  • Arrays: In C, an array name is a pointer to the first element of the array. The developer can use pointer arithmetic to access elements of an array.
  • Pointers to Functions: The developer can define pointers to functions in C, which can be used to pass functions as arguments to other functions, or to create callback functions.
  • Strings: In C, strings are arrays of characters. Pointers are commonly used to manipulate strings by accessing individual characters in the string.

FAQs

1. What is a pointer in C and its types?

In C, a variable that stores the memory address of another variable is known as a pointer. Depending on the kind of data they point to, pointers can be classified as int pointers, float pointers, char pointers, and more.

2. What is the syntax of a pointer?

In C, a pointer is declared using the syntax:

data_type *pointer_variable;

3. What is a null pointer and its syntax?

In C, a pointer that points to no memory location is known as a null pointer. The only syntax required is pointer_variable = NULL;, where pointer_variable denotes the name of the pointer.

4. What is an array of pointers?

In C, a pointer variable serves as each element of an array of pointers. It gives you the ability to store and manipulate data by allowing you to create arrays of pointers to various data kinds or structures.

5. How do you declare a pointer in C?

In C, you must declare a pointer by stating its data type, an asterisk (*), and the name of the pointer variable. As an example, "int *ptr;" declares an integer pointer with the name "ptr".

6. What are the advantages of pointers?

In C, pointers have a number of benefits, such as effective memory management, the ability to work with dynamic data structures like linked lists as well as trees, and the ability to send parameters by reference, allowing functions to modify the original data. They also make it possible to manipulate and access arrays more effectively.

Resources for further learning and practice

Official C Programming Documentation:

https://en.wikipedia.org/wiki/C_(programming_language)#:~:text=C%20is%20an%20imperative%20procedural,all%20with%20minimal%20runtime%20support. The official C Programming documentation is a comprehensive resource that covers all aspects of C Programming.

Summary

In summary, pointers are a potent tool in C that facilitate effective memory management, dynamic data structures, as well as code optimization. Programmers can deal with complex structures because they are able to understand their declaration, initialization, & dereferencing. Different pointer types are available in C, each having a particular purpose, such as passing function arguments or allocating memory. Obtaining a "C Programming Certification" can help you master these skills, as pointers have benefits, but they can also cause memory issues, making it essential to learn this skill.

Share
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like Java, Python, C, C++ etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like JavaScript and Cloud.

Accept cookies & close this