Paramclasses Academy - Blog

Tutorials Details

←
Post 13 of 15
→

What is Pointers in C Programming

What is Pointers in C Programming

October 14, 2026

1. Introduction to Pointers

Pointers are one of the most important concepts in C programming. They allow programs to work with memory addresses, access data indirectly, modify variables through their addresses, and manage dynamically allocated memory.

A normal variable stores a value, while a pointer stores the memory address of an object.

For example:

int number = 10;
int *ptr = &number;

Here:

  • number stores the value 10.

  • &number gives the memory address of number.

  • ptr stores that address.

  • *ptr accesses the value stored at that address.

In this module, you will learn:

  • What pointers are and why they are used

  • Pointer declaration and initialization

  • The address-of (&) and dereference (*) operators

  • Pointers and functions

  • Pointers and arrays

  • Pointer arithmetic

  • Null pointers and common mistakes

  • Practical programs and interview questions

2. What Is a Pointer in C?

A pointer is a variable that stores the address of an object or function, depending on its type.

Syntax

data_type *pointer_name;

Examples:

int *ptr;
char *charPtr;
float *floatPtr;

The pointer type indicates the type of object it is intended to point to.

For example, int *ptr declares a pointer to an integer.

3. Why Do We Use Pointers?

Pointers are useful for several reasons:

  1. Modify variables through functions: A function can receive a variable's address and modify the original variable.

  2. Work with arrays and strings: Pointers provide ways to access array elements and character data.

  3. Dynamic memory allocation: Functions such as malloc() and free() use pointers to manage allocated memory.

  4. Build data structures: Linked lists, trees, and graphs commonly use pointers to connect nodes.

  5. Avoid unnecessary copying: Functions can access larger objects through pointers instead of copying the entire object.

Real-world examples include dynamically sized collections, linked lists, file-processing buffers, and systems programming.

4. Understanding Memory Addresses

Consider this program:

#include <stdio.h>

int main(void)
{
    int number = 10;
    int *ptr = &number;

    printf("Value: %d\n", number);
    printf("Address: %p\n", (void *)&number);
    printf("Pointer value: %p\n", (void *)ptr);
    printf("Value through pointer: %d\n", *ptr);

    return 0;
}

Example output (the address will vary):

Value: 10
Address: 0x7ffe1234
Pointer value: 0x7ffe1234
Value through pointer: 10

The address shown above is illustrative, not a fixed address.

ptr

Stores the address of number

Points to

number = 10

The object stored at that address

Important: A pointer should only be dereferenced when it points to a valid object of the appropriate type. Never assume a memory address will be the same across program runs.

5. The Address-of Operator (&)

The address-of operator returns the address of an object.

Example:

#include <stdio.h>

int main(void)
{
    int age = 25;

    printf("Age: %d\n", age);
    printf("Address: %p\n", (void *)&age);

    return 0;
}

Output:

Age: 25
Address: 0x7ffe1234

The address is only an example; the actual address depends on the program's execution environment.

Here, &age means “the address of age.”

6. The Dereference Operator (*)

The dereference operator accesses the object to which a valid pointer points.

Example:

#include <stdio.h>

int main(void)
{
    int number = 50;
    int *ptr = &number;

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

    return 0;
}

Output:

Number: 50

The expression *ptr accesses the value of number.

The symbol * has two related uses:

  • In int *ptr;, it declares a pointer to an integer.

  • In *ptr, it dereferences the pointer.

7. Changing a Variable Through a Pointer

A pointer can be used to modify the object it points to.

#include <stdio.h>

int main(void)
{
    int number = 10;
    int *ptr = &number;

    *ptr = 100;

    printf("Number = %d\n", number);

    return 0;
}

Output:

Number = 100

Because ptr points to number, assigning 100 to *ptr changes the original variable.

8. Pointers and Functions

C passes arguments by value. However, passing an object's address allows a function to modify that object through a pointer.

Example: Swap two numbers

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void)
{
    int x = 10;
    int y = 20;

    swap(&x, &y);

    printf("x = %d\n", x);
    printf("y = %d\n", y);

    return 0;
}

Output:

x = 20
y = 10

How it works:

  1. swap(&x, &y) passes the addresses of x and y.

  2. a points to x, and b points to y.

  3. temp temporarily stores the value of x.

  4. The values are exchanged through the pointers.

This is a common practical application of pointers.

9. Pointers and Arrays

In many expressions, an array name is converted to a pointer to its first element. However, an array and a pointer variable are not the same thing.

Example:

#include <stdio.h>

int main(void)
{
    int numbers[] = {10, 20, 30, 40};
    int *ptr = numbers;

    printf("%d\n", *ptr);
    printf("%d\n", *(ptr + 1));
    printf("%d\n", *(ptr + 2));

    return 0;
}

Output:

10
20
30

Explanation:

  • ptr points to numbers[0].

  • *ptr accesses numbers[0].

  • *(ptr + 1) accesses numbers[1].

  • *(ptr + 2) accesses numbers[2].

Pointer arithmetic is scaled by the size of the pointed-to type, so ptr + 1 advances to the next integer element.

10. Pointer Arithmetic

Pointer arithmetic allows movement between elements of an array.

Valid operations include:

  • Incrementing a pointer to move to the next array element.

  • Decrementing a pointer to move to the previous element.

  • Adding or subtracting an integer offset.

  • Subtracting pointers that point into the same array, or one position past it.

Example:

#include <stdio.h>

int main(void)
{
    int numbers[] = {10, 20, 30};
    int *ptr = numbers;

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

    ptr++;

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

    ptr++;

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

    return 0;
}

Output:

10
20
30

Important: Pointer arithmetic must stay within the same array object or at most one position past its end. Dereferencing an out-of-bounds pointer is undefined behavior.

11. Null Pointers

A null pointer does not point to a valid object or function.

You can initialize a pointer to null when it does not yet point to an object.

#include <stdio.h>

int main(void)
{
    int *ptr = NULL;

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

    return 0;
}

Output:

Pointer is NULL.

A null pointer must not be dereferenced. Check it before accessing the object it is expected to point to.

12. Pointer to Pointer

A pointer to pointer stores the address of another pointer.

Syntax

int **ptr;

Example:

#include <stdio.h>

int main(void)
{
    int number = 25;
    int *ptr = &number;
    int **ptr2 = &ptr;

    printf("Number: %d\n", number);
    printf("Using pointer: %d\n", *ptr);
    printf("Using pointer to pointer: %d\n", **ptr2);

    return 0;
}

Output:

Number: 25
Using pointer: 25
Using pointer to pointer: 25

Here, ptr2 points to ptr, and *ptr2 accesses ptr. Therefore, **ptr2 accesses the original integer.

13. Dynamic Memory Allocation

C provides functions to allocate and release memory while a program is running.

These functions are declared in <stdlib.h>:

  • malloc() allocates a block of memory.

  • calloc() allocates memory and initializes all bytes to zero.

  • realloc() changes the size of an allocated block, when possible.

  • free() releases allocated memory.

Example: Using malloc()

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

int main(void)
{
    int *ptr = malloc(3 * sizeof *ptr);

    if (ptr == NULL)
    {
        printf("Memory allocation failed.\n");
        return 1;
    }

    ptr[0] = 10;
    ptr[1] = 20;
    ptr[2] = 30;

    printf("%d %d %d\n", ptr[0], ptr[1], ptr[2]);

    free(ptr);
    ptr = NULL;

    return 0;
}

Output:

10 20 30

Important practices:

  • Check whether allocation succeeded.

  • Access only the allocated elements.

  • Call free() when the allocated memory is no longer needed.

  • Do not access memory after it has been freed.

  • Do not free the same allocation more than once.

14. Common Pointer Mistakes

Mistake 1: Using an uninitialized pointer

Incorrect:

int *ptr;
*ptr = 10;

ptr has not been initialized to point to a valid object.

Correct:

int number;
int *ptr = &number;

*ptr = 10;

Mistake 2: Dereferencing a null pointer

Incorrect:

int *ptr = NULL;
printf("%d\n", *ptr);

Correct:

if (ptr != NULL)
{
    printf("%d\n", *ptr);
}

Mistake 3: Returning the address of a local variable

A local variable's lifetime ends when its function returns. Returning its address and then using that pointer can result in undefined behavior.

Mistake 4: Accessing memory after free()

Once dynamically allocated memory has been freed, pointers to that allocation must not be used to access it.

Mistake 5: Going beyond array bounds

Pointer arithmetic and dereferencing must stay within valid array boundaries.

15. Practical Programs for Practice

Try writing these programs yourself:

  1. Print a variable's value and address.

  2. Change a variable's value using a pointer.

  3. Swap two numbers using pointers.

  4. Access array elements using pointer arithmetic.

  5. Find the sum of array elements using a pointer.

  6. Find the maximum array element using a pointer.

  7. Demonstrate a pointer to pointer.

  8. Allocate an integer array using malloc().

  9. Calculate the string length by traversing a character array with a pointer.

  10. Write a function that updates a variable through a pointer.

16. Interview Questions

Q1. What is a pointer in C?

A pointer is a variable that stores a memory address.

Q2. What does the & operator do?

It obtains the address of an object.

Q3. What does the * operator do with a pointer expression?

It dereferences a valid pointer to access the pointed-to object.

Q4. What is a null pointer?

A pointer value that does not point to a valid object or function.

Q5. What is pointer arithmetic?

Operations such as incrementing or adding an offset to navigate between elements of an array.

Q6. What is a pointer to pointer?

A pointer that stores the address of another pointer.

Q7. Why do we use malloc()?

It dynamically allocates a block of memory and returns a pointer to it on success.

Q8. What is a dangling pointer?

A pointer that refers to an object or allocation whose lifetime has ended or which has been released.

Q9. What is the relationship between arrays and pointers?

In many expressions, an array converts to a pointer to its first element, but an array is not itself a pointer variable.

Q10. What is the difference between NULL and an uninitialized pointer?

NULL explicitly represents a null pointer value. An uninitialized automatic pointer has an indeterminate value and must not be used.

17. Quick Revision

  • Pointers store addresses.

  • & obtains an object's address.

  • * dereferences a valid pointer.

  • Pointers can modify objects through their addresses.

  • Array elements can be accessed using pointers.

  • Pointer arithmetic must respect array boundaries.

  • A null pointer must not be dereferenced.

  • malloc() and related functions support dynamic memory allocation.

  • Dynamically allocated memory should be released using free().

  • Invalid pointer use can cause crashes or undefined behavior.

18. Homework

  1. Define a pointer and explain its uses.

  2. Write a program to print the address of an integer variable.

  3. Write a program to swap two numbers using pointers.

  4. Explain the difference between & and *.

  5. Write a program to calculate an array's sum using pointer arithmetic.

  6. Explain null pointers and dangling pointers.

  7. Write a program using malloc() and free().

  8. Explain the difference between an array and a pointer.

Conclusion

In this module, you learned pointer fundamentals, memory addresses, dereferencing, pointer arithmetic, pointers with functions and arrays, pointer-to-pointer declarations, and dynamic memory allocation.

←
Post 13 of 15
→