Pointers in C

A Pointer in C is a derived data type whose value is the memory address of another variable. Instead of holding direct data (like an integer or character), a pointer stores the physical RAM location where that data resides. Pointers are one of the most powerful and distinctive features of C programming, enabling direct memory manipulation, dynamic memory allocation, and efficient data structures like linked lists, trees, and graphs.

Why Do We Use Pointers in C?

 Direct Memory Access: Pointers allow programs to directly access and manipulate hardware and system memory.

 Dynamic Memory Allocation: Essential for library functions like malloc(), calloc(), and realloc().

 Call by Reference: Allows functions to modify variables declared in the calling scope.

 Performance & Efficiency: Passing a 4-byte or 8-byte pointer to a large structure or array avoids expensive data copying in memory.

Pointer Declaration and Two Fundamental Operators

To work with pointers, C provides two dedicated unary operators:

 Address-Of Operator (&): Returns the memory address of a variable.

 Dereference / Indirection Operator (*): Accesses the value stored at the memory address held by the pointer.

Syntax of Pointer Declaration


data_type *pointer_name;
							

 The * informs the compiler that pointer_name is a pointer variable, not an ordinary variable.

 The data_type denotes the type of data stored at the target memory location.


int *p_int;       /* Pointer to an integer */
float *p_float;   /* Pointer to a float */
char *p_char;     /* Pointer to a character */
							

Initialization of Pointer Variables

A pointer must always be assigned a valid memory address before dereferencing it:


int age = 21;     /* Normal variable */
int *ptr;         /* Pointer declaration */

ptr = &age;       /* Pointer initialization: ptr now holds address of age */
							

Comprehensive Working Example: Pointers in Action


#include <stdio.h>

int main()
{
    int no = 10;
    int *ptr;

    /* Assigning address of no to ptr */
    ptr = &no;

    printf("Value of variable 'no'       : %d\n", no);
    printf("Memory address of 'no' (&no) : %p\n", (void*)&no);
    printf("Address stored in 'ptr'      : %p\n", (void*)ptr);
    printf("Value pointed to by 'ptr' (*ptr): %d\n", *ptr);

    /* Modifying value via pointer */
    *ptr = 25;
    printf("\nAfter modifying *ptr = 25:\n");
    printf("New value of 'no'            : %d\n", no);

    return 0;
}
							

Pointer Arithmetic

In C, arithmetic operations on pointers depend entirely on the byte size of the underlying data type:

 Increment (ptr++): Increases the address by sizeof(data_type). If an int* holds address 1000 on a 4-byte system, ptr++ produces 1004.

 Decrement (ptr--): Decreases the address by sizeof(data_type).

 Adding/Subtracting an integer (ptr + n): New Address = Current Address + (n * sizeof(type)).

 Subtracting two pointers: Yields the number of elements separating them, provided both point to elements of the same array.

Call by Reference: Swapping Two Numbers (BCA Classic)

By passing addresses to a function, the function can directly alter the caller's variables:


#include <stdio.h>

/* Function to swap values using pointers */
void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a = 10, b = 20;

    printf("Before swap: a = %d, b = %d\n", a, b);
    swap(&a, &b); /* Passing addresses */
    printf("After swap : a = %d, b = %d\n", a, b);

    return 0;
}
							

Pointer to Pointer (Double Pointer)

A double pointer (**ptr) is a pointer variable that stores the memory address of another pointer variable:


int val = 50;
int *p1 = &val;    /* p1 points to val */
int **p2 = &p1;    /* p2 points to p1 */

printf("Value: %d\n", **p2); /* Prints 50 */
							

Special Types of Pointers (Viva Must-Knows)

Pointer Type

Definition & Usage

NULL Pointer

A pointer initialized to NULL (address 0) indicating it points to nothing. Prevents illegal access: int *p = NULL;

Void Pointer (void*)

A generic pointer with no associated data type. Used by malloc(). Must be typecast before dereferencing.

Wild Pointer

An uninitialized pointer pointing to an arbitrary/unknown memory location. Extremely dangerous.

Dangling Pointer

A pointer pointing to a memory location that has already been deallocated or freed.


Practice Session for Beginner Students

Solidify your pointer mastery with these output prediction challenges, MCQs, and hands-on lab assignments.

Part 1: Predict the Output (Dry-Run Practice)

Problem 1: What will be the output of the following C program?


#include <stdio.h>
int main() {
    int arr[] = {10, 20, 30};
    int *p = arr;
    printf("%d ", *p);
    p++;
    printf("%d ", *p);
    return 0;
}
							

Answer & Explanation: Output is 10 20. Initially, p points to arr[0] (value 10). When p++ executes, p moves forward by sizeof(int) bytes to point to arr[1] (value 20).

Problem 2: What is the output of (*p)++ vs *p++?

Answer & Explanation: (*p)++ increments the value pointed to by p. On the other hand, *p++ dereferences the current address and then increments the pointer address itself due to operator precedence.

Part 2: Multiple Choice Questions (BCA Exam Preparation)

Q1. Which operator is used to obtain the memory address of a variable?
A) *
B) &
C) ->
D) %
Correct Answer: B) & (Address-of operator).

Q2. What is the size of a pointer variable in C?
A) Always 2 bytes
B) Always 4 bytes
C) Dependent on the architecture (4 bytes on 32-bit systems, 8 bytes on 64-bit systems)
D) Same as the size of the data type it points to
Correct Answer: C) Dependent on machine architecture (4 bytes on 32-bit, 8 bytes on 64-bit) regardless of whether it points to a char, int, or double.

Part 3: Hands-on Lab Assignments for Beginners

Assignment 1: Write a C program to traverse and print all elements of an array using pointer notation instead of bracket indexing (*(arr + i)).
Logic Hint: In C, arr[i] is internally converted to *(arr + i) by the compiler.

Assignment 2: Write a program to find the length of a string using a character pointer.
Logic Hint: Initialize char *p = str; int count = 0; while(*p != '\0') { count++; p++; }.

Part 4: Frequently Asked University Viva Questions

1. What is a dangling pointer and how do you prevent it?
Answer: A dangling pointer occurs when a pointer points to memory that has been released via free(). You prevent it by setting the pointer to NULL immediately after freeing: free(ptr); ptr = NULL;.

2. Can we perform multiplication or division on pointer addresses?
Answer: No. C allows only addition and subtraction of integers with pointers, and subtraction of two pointers of the same type. Multiplying or dividing memory addresses is strictly illegal.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!