Skip to main content
C Programming Basics
CHAPTER 12 Beginner

Pointers in C

Updated: May 17, 2026
5 min read

# CHAPTER 12

Pointers in C

1. Introduction

Pointers are often considered the hardest part of C, but they are also its greatest strength. A pointer is simply a variable that stores the memory address of another variable. Once you master pointers, you understand how computer memory truly works.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain what memory addresses are.
  • Declare and initialize pointers.
  • Use the & (address-of) and * (dereference) operators.
  • Pass variables to functions by reference using pointers.
  • Understand the Null pointer.

3. Memory Addresses (The & Operator)

Every variable is stored in a specific location in RAM, identified by a hexadecimal address. You can find this address using the & (address-of) operator.
c
12345678
#include <stdio.h>

int main() {
    int age = 25;
    printf("Value of age: %d\n", age);
    printf("Memory address of age: %p\n", &age); // %p prints pointer address
    return 0;
}

*Sample Output:*

text
12
Value of age: 25
Memory address of age: 0x7ffee1b2c

4. Declaring and Using Pointers

To store a memory address, you must use a pointer variable. We declare a pointer using the * symbol.
c
12345
int age = 25;
int *ptr;      // Declares a pointer to an integer
ptr = &age;    // Stores the address of 'age' in 'ptr'

printf("Address stored in ptr: %p\n", ptr);

5. Dereferencing (The * Operator)

If you have a pointer, you can access or modify the value at that memory address using the * operator (called the dereference operator).
c
1234567
int age = 25;
int *ptr = &age;

printf("Value via pointer: %d\n", *ptr); // Dereferences ptr to get 25

*ptr = 30; // Changes the value at the address to 30!
printf("New age: %d\n", age); // age is now 30

6. Visualizing Pointers in Memory

12345678
int age = 25;
int *ptr = &age;

Memory:
+---------+         +----------------+
| ptr     | ------> | age = 25       |
| (0x500) |         | (Address 0x100)|
+---------+         +----------------+
  • &age is 0x100.
  • ptr holds 0x100.
  • *ptr goes to 0x100 and grabs 25.

7. Pass by Reference (Modifying values in functions)

In Chapter 9, we learned that C uses "pass by value" (it copies data to functions). If a function wants to modify the original variable, it needs the memory address.
c
123456789101112131415161718
#include <stdio.h>

// Function takes pointers (memory addresses)
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    
    // We pass the addresses of x and y
    swap(&x, &y);
    
    printf("x = %d, y = %d\n", x, y); // x = 20, y = 10
    return 0;
}

8. Null Pointers

Always initialize pointers! If a pointer doesn't have an address to point to yet, assign it NULL. An uninitialized pointer (Wild Pointer) points to a random memory address, which can crash your program.
c
1
int *ptr = NULL; // Safe

9. Common Mistakes

  • Confusing * in declaration vs usage:
int *ptr; (Declaration: ptr is a pointer to an int). *ptr = 10; (Usage: put 10 into the memory address ptr points to).
  • Dereferencing uninitialized pointers: int *p; *p = 5; -> Segmentation Fault!
  • Type mismatch: Assigning the address of a float to an int * pointer.

10. Exercises

  1. 1. Write a program to find the maximum of two numbers using pointers.
  1. 2. Write a function void increment(int *n) that adds 1 to the variable passed to it.
  1. 3. Print the memory addresses of an integer, a float, and a char.

11. MCQ Quiz with Answers

Question 1

What does the & operator do?

Question 2

What does the * operator do when used in an expression (not declaration)?

Question 3

Which format specifier is used to print a memory address?

Question 4

What is a Null pointer?

Question 5

Why do we pass pointers to functions (swap(&x, &y))?

Question 6

What happens if you dereference an uninitialized pointer?

Question 7

If int a = 10; int *p = &a;, what does *p equal?

Question 8

Pointers allow C to interact directly with:

Q9. Can a pointer point to another pointer? a) Yes b) No Answer: a) Yes
Question 10

What does int *ptr; mean?

12. Interview Questions

  • Q: Explain the difference between *ptr and &ptr.
  • Q: What is a Wild Pointer, and how do you avoid it?
  • Q: How do pointers enable "pass by reference" behavior in C?

13. Summary

Pointers store memory addresses. The & operator gets the address of a variable, while the * operator gets the value at an address (dereferencing). Pointers are crucial for modifying variables inside functions, dynamic memory allocation, and array manipulation.

14. Next Chapter Recommendation

Now that you know the basics of pointers, in Chapter 13: Pointer Arithmetic, we will learn how to do math with memory addresses to navigate through arrays seamlessly.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·