Chapter 9 – Pointers in C Language
Introduction
Pointers are one of the most important concepts in C programming. They provide a way to work directly with memory addresses and allow programs to access and modify data indirectly.
Pointers are used extensively with arrays, strings, functions, dynamic memory allocation, structures, and many other advanced C programming concepts.
Pointers may seem difficult at first, but understanding two basic ideas makes them much easier:
Every variable is stored somewhere in memory.
A pointer can store the address of another variable.
In this chapter, you will learn about memory addresses, pointer declaration, the address-of operator &, the dereference operator *, pointers and arrays, pointers and functions, and common pointer mistakes.
1. What is a Pointer?
A pointer is a variable that stores the memory address of another variable.
For example:
int number = 25;
The variable number contains the value 25, and that value is stored at some location in computer memory.
A pointer can store the address of number.
int *ptr = &number;
Here:
number stores 25.
&number gives the address of number.
ptr stores that address.
*ptr accesses the value stored at that address.
2. Understanding Memory
When a program runs, variables are stored in memory.
Consider:
int number = 25;
Conceptually:
Memory Address Value +----------+ +------+ | 1000 | ---> | 25 | +----------+ +------+ ↑ number
The actual memory address is determined by the system and may be different each time the program runs.
A pointer allows us to work with such addresses.
3. Address-of Operator &
The address-of operator & is used to obtain the memory address of a variable.
Example:
#include <stdio.h> int main() { int number = 25; printf("Value = %d\n", number); printf("Address = %p\n", (void *)&number); return 0; }
The address will vary depending on the system and execution.
Important
Use %p for displaying a pointer address, typically with a cast to (void *).
4. Declaring a Pointer
The general syntax is:
data_type *pointer_name;
Examples:
int *ptr; float *ptr; char *ptr; double *ptr;
The data type tells C what kind of object the pointer is intended to point to.
5. Assigning an Address to a Pointer
Example:
#include <stdio.h> int main() { int number = 50; int *ptr; ptr = &number; printf("Value = %d", number); return 0; }
Here:
ptr → address of number
The pointer does not contain the value 50; it contains the address where number is stored.
6. Dereference Operator *
The * operator is used to access the value stored at the address contained in a pointer.
Example:
#include <stdio.h> int main() { int number = 50; int *ptr = &number; printf("Value = %d", *ptr); return 0; }
Output
Value = 50
Here:
*ptr
means:
Access the value stored at the memory location pointed to by ptr.
7. & and * Operators
These two operators are very important.
| Operator | Meaning |
|---|---|
| & | Gets the address of a variable |
| * | Dereferences a pointer to access the pointed-to value |
Example:
int number = 100; int *ptr = &number;
Conceptually:
number ↓ +-------+ | 100 | +-------+ ↑ | ptr
Therefore:
&number
gives the address of number, while:
*ptr
gives the value stored at that address.
8. Changing a Value Using a Pointer
A pointer can be used to modify the value of a variable.
#include <stdio.h> int main() { int number = 10; int *ptr = &number; *ptr = 50; printf("Number = %d", number); return 0; }
Output
Number = 50
The statement:
*ptr = 50;
changes the value of number because ptr points to number.
9. Pointer Example
#include <stdio.h> int main() { int number = 25; int *ptr = &number; printf("Number = %d\n", number); printf("Value using pointer = %d\n", *ptr); printf("Address = %p\n", (void *)ptr); return 0; }
This demonstrates three related concepts:
Direct access to the variable.
Indirect access through the pointer.
Access to the variable's address.
10. Pointer Data Types
Pointers can point to different types of variables.
Integer Pointer
int number = 10; int *ptr = &number;
Character Pointer
char grade = 'A'; char *ptr = &grade;
Float Pointer
float price = 25.5f; float *ptr = &price;
The pointer type should be compatible with the object it points to.
11. NULL Pointer
A pointer that does not currently point to a valid object can be initialized with NULL.
Example:
int *ptr = NULL;
A null pointer represents the absence of a valid target.
It is important not to dereference a null pointer.
Incorrect:
*ptr = 10;
when:
ptr == NULL
This can cause undefined behavior.
12. Pointer and Array Relationship
Arrays and pointers are closely related in C.
Consider:
int numbers[5] = {10, 20, 30, 40, 50};
The array name is commonly converted to a pointer to its first element in expressions.
For example:
int *ptr = numbers;
Now ptr points to the first element.
numbers ↓ +----+----+----+----+----+ | 10 | 20 | 30 | 40 | 50 | +----+----+----+----+----+ ↑ ptr
13. Accessing Array Elements Using a Pointer
Example:
#include <stdio.h> int main() { int numbers[5] = {10, 20, 30, 40, 50}; int *ptr = numbers; int i; for(i = 0; i < 5; i++) { printf("%d ", *(ptr + i)); } return 0; }
Output
10 20 30 40 50
The expression:
*(ptr + i)
accesses the element at position i.
14. Pointer Arithmetic
C allows arithmetic operations on pointers to elements of arrays.
Suppose:
int numbers[3] = {10, 20, 30}; int *ptr = numbers;
Then:
ptr
points to the first element.
ptr + 1
points to the next element.
ptr + 2
points to the third element.
The actual address difference depends on the size of the pointed-to type.
15. Incrementing a Pointer
Example:
#include <stdio.h> int main() { int numbers[] = {10, 20, 30}; int *ptr = numbers; printf("%d\n", *ptr); ptr++; printf("%d\n", *ptr); return 0; }
Output
10 20
After ptr++, the pointer moves to the next element of the array.
16. Pointers and Functions
Pointers can be passed to functions when the function needs to access or modify an object owned by the caller.
This is especially useful when a function needs to modify more than one value.
Example:
#include <stdio.h> void changeValue(int *ptr) { *ptr = 100; } int main() { int number = 20; changeValue(&number); printf("Number = %d", number); return 0; }
Output
Number = 100
The address of number is passed to the function.
17. Swapping Two Numbers Using Pointers
One common application of pointers is swapping values.
#include <stdio.h> void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } int main() { 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
The function modifies the original variables through their addresses.
18. Pointer to Pointer
A pointer can itself have an address, so C also allows a pointer to another pointer.
Example:
int number = 10; int *ptr = &number; int **ptr2 = &ptr;
Conceptually:
number ↑ ptr ↑ ptr2
Here:
number stores 10.
ptr stores the address of number.
ptr2 stores the address of ptr.
19. Accessing a Value Through a Pointer to Pointer
#include <stdio.h> int main() { int number = 10; int *ptr = &number; int **ptr2 = &ptr; printf("%d\n", number); printf("%d\n", *ptr); printf("%d\n", **ptr2); return 0; }
Output
10 10 10
The expression:
**ptr2
ultimately accesses the value stored in number.
20. Character Pointers and Strings
Pointers are also frequently used with strings.
Example:
#include <stdio.h> int main() { char *message = "Hello"; printf("%s", message); return 0; }
Here, message points to the first character of a string literal.
A string literal should not be modified through this pointer.
For a modifiable character array, use:
char message[] = "Hello";
21. Pointer to a Character Array
Example:
#include <stdio.h> int main() { char text[] = "Computer"; char *ptr = text; while(*ptr != '\0') { printf("%c", *ptr); ptr++; } return 0; }
Output
Computer
The pointer moves through the character array until it reaches '\0'.
22. Pointers and Arrays – Important Relationship
Consider:
int numbers[] = {10, 20, 30};
These expressions access the first element:
numbers[0]
and:
*(numbers + 0)
For later elements:
numbers[1]
corresponds to:
*(numbers + 1)
Similarly:
numbers[2]
corresponds to:
*(numbers + 2)
This relationship is important when working with arrays and pointers.
23. const and Pointers
The const keyword can be used to prevent modification through a pointer.
Example:
int number = 10; const int *ptr = &number;
Through ptr, the value cannot be modified:
/* *ptr = 20; */ /* Not allowed */
But the original variable can still be changed directly:
number = 20;
Pointers with const become especially important when passing strings and arrays to functions.
24. Common Pointer Mistakes
Mistake 1: Using an uninitialized pointer
Incorrect:
int *ptr; *ptr = 10;
The pointer has not been given a valid target.
A safer approach is:
int number; int *ptr = &number; *ptr = 10;
Mistake 2: Dereferencing NULL
Incorrect:
int *ptr = NULL; printf("%d", *ptr);
A null pointer must not be dereferenced.
Mistake 3: Using an invalid address
A pointer should point to a valid object or valid allocated memory before it is dereferenced.
Mistake 4: Going outside an array
Pointer arithmetic must stay within the appropriate array object.
25. Advantages of Pointers
Pointers provide several important capabilities:
They allow indirect access to variables.
They can be used to modify values through functions.
They work closely with arrays and strings.
They are important for dynamic memory allocation.
They are used in structures and linked data structures.
They can improve flexibility when handling memory and data.
26. Important Pointer Operators
| Operator | Meaning |
|---|---|
| & | Address-of operator |
| * | Dereference operator |
| -> | Access structure member through a pointer |
| ++ | Can move a pointer to the next array element |
The -> operator will be discussed in greater detail when structures and structure pointers are introduced.
27. Pointer Example – Complete Program
#include <stdio.h> int main() { int number = 100; int *ptr = &number; printf("Value of number = %d\n", number); printf("Value using pointer = %d\n", *ptr); printf("Address of number = %p\n", (void *)&number); printf("Address stored in pointer = %p\n", (void *)ptr); return 0; }
The two printed addresses should represent the same location for this example.
28. Why Are Pointers Important?
Pointers are a major feature of C because they provide a way to work with memory addresses.
They are especially important for:
Arrays
Strings
Functions
Dynamic memory allocation
Structures
Linked lists
Trees
System programming
Low-level programming
A strong understanding of pointers is therefore essential before moving to advanced C programming.
29. Quick Revision
| Concept | Meaning |
|---|---|
| Pointer | Variable that stores an address |
| & | Gets the address of a variable |
| * | Accesses the value at an address |
| NULL | Represents no valid target |
| Pointer Arithmetic | Moving between compatible array elements |
| int *p | Pointer to an integer |
| char *p | Pointer to a character |
| int **p | Pointer to a pointer |
| Dereferencing | Accessing the value through a pointer |
30. Practice MCQs
Question 1
What does a pointer store?
A. Only a character
B. A memory address
C. Only a constant
D. A function name only
Answer: B. A memory address
Question 2
Which operator is used to obtain the address of a variable?
A. *
B. &
C. %
D. #
Answer: B. &
Question 3
Which operator is used to dereference a pointer?
A. &
B. @
C. *
D. %
Answer: C. *
Question 4
What does the following declaration represent?
int *ptr;
A. An integer variable
B. A pointer to an integer
C. A pointer to a character
D. An array of integers
Answer: B. A pointer to an integer
Question 5
Which value is commonly used to represent a pointer that points to no valid object?
A. 0.0
B. NULL
C. false only
D. EOF
Answer: B. NULL
Question 6
If:
int x = 10; int *p = &x;
what is the value of *p?
A. Address of x
B. 0
C. 10
D. Address of p
Answer: C. 10
Question 7
Which is commonly used to pass the address of a variable to a function?
A. &
B. #
C. @
D. %
Answer: A. &
31. Programming Exercises
Try writing C programs to:
Declare an integer pointer and display the value of a variable.
Display the address of a variable using %p.
Modify a variable using a pointer.
Swap two numbers using pointers.
Find the sum of two numbers using pointers.
Display array elements using pointer arithmetic.
Find the largest element of an array using a pointer.
Count characters in a string using a character pointer.
Reverse a string using a pointer.
Create a function that modifies two variables using pointer parameters.
Demonstrate a pointer to a pointer.
Write a program that traverses an integer array using a pointer.
32. Key Points to Remember
A pointer stores the address of an object.
The & operator obtains an address.
The * operator dereferences a pointer.
A pointer should point to a valid object before it is dereferenced.
NULL represents a null pointer.
Arrays and pointers are closely related in C.
Pointers can be passed to functions to allow the function to modify caller-owned objects.
Pointer arithmetic is commonly used when traversing arrays.
Character pointers can be used to traverse strings.
Pointer-to-pointer variables store the address of another pointer.
Careful pointer usage is essential because invalid memory access can lead to undefined behavior.
Chapter Summary
Pointers allow C programs to work with memory addresses and access data indirectly. A pointer can store the address of a variable, and the dereference operator can be used to access the value at that address.
Pointers are closely connected with arrays, strings, and functions. They also form the foundation for more advanced topics such as dynamic memory allocation, structures, linked lists, and other data structures.
Once you understand the relationship between a variable, its address, and a pointer, many advanced C programming concepts become easier to understand.
Next Chapter
Chapter 10 – Structures and Unions in C Language
In the next chapter, you will learn how to create custom data structures using struct, access structure members, create arrays of structures, use structure pointers, and understand the difference between structures and unions.