A Beginner's Guide to Pointers in C: Understanding and Application

A Beginner's Guide to Pointers in C: Understanding and Application

Introducing the C programming language

The C language was developed in 1972 at At&T Bell Labs by Dennis Ritchie. It combines features of the B and BCPL (Basic Combined Programming Language) programming languages but is also mixed with the Pascal language. C was used to create the Unix operating system.

Pointers in C

Pointers are one of the fundamental and toughest concepts to grasp in the C programming language. Even experienced programmers still have issues with it, but in this article, we will demystify and break it into bits so that even a toddler will be able to grasp the concept.

A pointer is a variable that holds the address of another variable. A variable is a datatype in C; examples of variables are int, char, double, float, etc., while the address is the memory location on a computer where it is stored. Just imagine you are going to Mr. A’s house, but instead of going straight to his house, you decide to first visit Mr. B’s house since he holds the address of Mr. A’s house.

Pointers Declaration

Pointers are declared using the dereference operator (*)

Datatype *ptr

int *ptr

char *ptr

float *ptr

double *ptr

void *ptr

it can also be written as datatype* ptr

Initialization of Pointers

Pointers are initialized using the address-of-operator, also called the ampersand (&). Let us use a code example to show the initialization of pointers.

#include <stdio.h>

int main()
{
    int a = 20; //declaration of a int variable
    int *ptr; //declaration of a pointer variable
    ptr = &a; //initializing ptr, ptr now holds the address of a
    printf("the address of p is %p\n", ptr);
    printf("the address of a is %p\n", &a);
    return (0);
}

When you run and compile this code, you will see that the addresses of a and ptr are the same.

Pointer Arithmetic

The only arithmetic that can be done with a pointer is addition and subtraction.

that is incrementing and decrementing a pointer. To explain this again, let's see a simple code example.

int main()
{
    int array[] = {1, 2, 3, 4, 5};  // declaration of an array
    int *p; //declaring a pointer p
    p = &array;

    printf("the first element is %d\n", *p); // 1 is printed
    printf("the second element is %d\n", *(p + 1)); // 2 is printed
    printf("the third element is %d\n", *(p + 2)); // 3 is printed
    //lets now look at incrementing and decrementing
    p++;
    printf(" after incrementing %d\n", *p); // 2 is printed
    p--;
    printf(" after decrementing %d\n", *p); // 1 is printed
}

Run and compile the code to test it out.

Note: A void pointer cannot be incremented, decremented, or dereferenced.

Pointers and Arrays

Pointers and arrays share a close relationship in C. In fact, arrays can be accessed and manipulated using pointers, and vice versa.

#include <stdio.h>

int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6};
    int* p; //dont fret it is still the same as int *p
    p = &arr;

    printf("Using pointer to print the element of the array");
    printf("the first element is %d"\n, *p); //it can also be written as *(p + 0)
    printf("the second element is %d\n", *(p + 1)); // 2 is printed
    printf("the third element is %d\n", *(p + 2)); // 3 is printed
    return (0);
}

To print all the elements of the array, we can follow these simple code implementations.

#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6}
    int* p; //dont fret it is still the same as int *p
    p = &arr; //initializing the pointer
    int i; // to iterate through the array

    for (i = 0; i < 5; i++)
    {
        printf("Element %d = %d", i, *(p + i));
        ptr++; //move to the next element
    }
    return (0);
}

Pointers and memory allocation

Pointers are a very important concept when dealing with memory allocation in C programming. Functions like malloc, calloc, realloc, and free enable programmers to allocate memory for a program. We will need a pointer to easily allocate memory. To fully understand how it works, let's see a code example.


    int size = 5;
    int *ptr;

    ptr =(int *)malloc(size * sizeof(int)) // we have allocated memory to ptr
    free(ptr);

Memory allocation is not within the scope of this article; you can follow me so you won't miss out when I drop the article on memory allocation.

Pointers and structures

Pointers can also be used with structures to manipulate members of the

structure effectively.

struct test{
    int a;
    int b;
}
struct test test1;
struct test *ptr;
ptr = &test1;

ptr->a = 1;
ptr->b = 2;

printf("a and b are %d & %d\n", test1.a, test1.b);

Pointers with Functions (function pointers)

Pointers can also be used with functions to easily manipulate multiple functions in C programming.

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    int (*operation)(int, int);

    // Initializing function pointer with the 'add' function
    operation = &add;
    printf("Result: %d\n", operation(5, 3));

    // Reassigning the function pointer to the 'subtract' function
    operation = &subtract;
    printf("Result: %d\n", operation(5, 3));

    return 0;
}

Pointers are very effective as they hold the address of a variable.

Other examples of pointers are:

  1. Null pointer

  2. Dangling pointer

  3. Far pointer

  4. Wild pointer

Merits of using pointers

  1. pointers enable us to efficiently allocate memory

  2. they provide direct memory access

  3. pointers can be used for efficient array manipulation

The merits of pointers

  1. uninitialized pointers can cause segmentation fault

  2. pointers are a little bit complex to understand

The concept of pointers is tricky, especially for beginners. If you are still having issues, I recommend watching this tutorial on pointers by freecodecamp.org.

Thanks! You can follow me so you won't miss out on my next article.