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
*Sample Output:*
text
4. Declaring and Using Pointers
To store a memory address, you must use a pointer variable. We declare a pointer using the* symbol.
c
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
6. Visualizing Pointers in Memory
-
&ageis0x100.
-
ptrholds0x100.
-
*ptrgoes to0x100and grabs25.
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
8. Null Pointers
Always initialize pointers! If a pointer doesn't have an address to point to yet, assign itNULL. An uninitialized pointer (Wild Pointer) points to a random memory address, which can crash your program.
c
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
floatto anint *pointer.
10. Exercises
- 1. Write a program to find the maximum of two numbers using pointers.
-
2.
Write a function
void increment(int *n)that adds 1 to the variable passed to it.
- 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:
Question 10
What does int *ptr; mean?
12. Interview Questions
-
Q: Explain the difference between
*ptrand&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.