Pointer Arithmetic in C
Hello there! Welcome to this tutorial on “Pointer Arithmetic in C”. If you’re here, you’re probably curious about pointers, those mysterious entities in C that hold addresses rather than values. Well, you’re in the right place! Let’s dive in, shall we?
Table of Contents
Understanding Pointers
First things first, what’s a pointer? In C, a pointer is a variable that stores the memory address of another variable. This means that instead of holding a specific value, like an integer or a character, a pointer points to the location of a value in memory.
The Magic of Pointer Arithmetic
Now, here’s where things get interesting. In C, you can perform arithmetic operations on pointers, just like you can with regular numbers. This is known as pointer arithmetic. But wait, how can you perform arithmetic on addresses? Good question! Let’s find out.
Increment and Decrement of a Pointer
When you increment a pointer, you’re essentially moving the pointer to the next memory location. For instance, if you have a pointer ptr
pointing to an integer, doing ptr++
will move the pointer to the next integer in memory. Similarly, ptr--
will move the pointer to the previous integer.
Here’s a code example to illustrate this:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
printf("Before increment: %d\n", *ptr);
ptr++; // Now, ptr points to the second element of arr
printf("After increment: %d\n", *ptr);
return 0;
}
CAddition and Subtraction of an Integer to a Pointer
You can also add or subtract an integer to a pointer. When you do this, the pointer moves forward or backward by a number of memory locations equal to the integer. For instance, ptr + 2
will move the pointer two memory locations forward.
Here’s a code example to illustrate this:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
printf("Before addition: %d\n", *ptr);
ptr = ptr + 2; // Now, ptr points to the third element of arr
printf("After addition: %d\n", *ptr);
return 0;
}
CSubtracting Two Pointers
Subtracting two pointers gives you the number of elements between them. For instance, if ptr1
and ptr2
are pointers to elements in the same array, ptr2 - ptr1
will give you the number of elements between the elements pointed to by ptr1
and ptr2
.
Here’s a code example to illustrate this:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr1 = &arr[0]; // ptr1 points to the first element of arr
int *ptr2 = &arr[3]; // ptr2 points to the fourth element of arr
int diff = ptr2 - ptr1;
printf("Difference between pointers: %d\n", diff);
return 0;
}
CCode Examples
Let’s look at a couple of complete C codes to understand pointer arithmetic better.
Code Example 1: Traversing an Array Using Pointer Arithmetic
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of arr
// Print all elements of arr using pointer arithmetic
for(int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
CIn this code, we’re using pointer arithmetic to traverse an array. The pointer ptr
points to the first element of the array arr
. In the for
loop, we’re incrementing the pointer by i
each time, effectively moving the pointer to the i
-th element of the array. We then dereference the pointer using the *
operator to get the value of the i
-th element, which we print.
The output of this code will be: 1 2 3 4 5
.
Code Example 2: Swapping Two Numbers Using Pointers and Pointer Arithmetic
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 5, num2 = 10;
swap(&num1, &num2);
printf("num1 = %d, num2 = %d", num1, num2);
return 0;
}
CIn this code, we’re using pointers to swap two numbers. The swap
function takes two pointers as arguments. It dereferences these pointers to get the values they point to, swaps these values, and then assigns the swapped values back to the memory locations pointed to by the pointers. This effectively swaps the values of the two numbers in the main
function.
The output of this code will be: num1 = 10, num2 = 5
.
Wrapping Up
And there you have it! You now know what pointer arithmetic is, how it works, and how to use it in your C programs. Remember, pointers are a powerful tool in C, and understanding how to use them effectively can make your code more efficient and easier to understand.
Frequently Asked Questions (FAQ)
What is a pointer arithmetic in C?
Pointer arithmetic involves performing arithmetic operations on pointers, such as incrementing or decrementing a pointer, or adding or subtracting an integer to a pointer.
What is pointer arithmetic with example?
An example of pointer arithmetic is incrementing a pointer to move it to the next element in an array. For instance, if
ptr
is a pointer to an integer array, doingptr++
will move the pointer to the next integer in the array.What is the purpose of pointer arithmetic?
The purpose of pointer arithmetic is to allow you to manipulate pointers to traverse and access elements in arrays and other data structures.
Which pointer arithmetic operator can be used in C?
The pointer arithmetic operators in C are
++
(increment),--
(decrement),+
(addition), and-
(subtraction).
Related Tutorials
- Understanding Pointers in C
- Pointer Arithmetic in C
- Pointers with Arrays in C
- Pointers with Strings in C
- Pointers with Functions in C
- C Dynamic Memory Allocation
Dr. Mehedi Hasan is a seasoned semiconductor professional, academic and web-designer with over a decade of experience in digital system design and verification as well as web development. Currently a Senior Engineer at AMD in Markham, Ontario, he plays a key role in the development and verification of cutting-edge chip technologies, earning multiple Spotlight Awards for his contributions.
Dr. Hasan holds a Ph.D. in Electrical and Computer Engineering from the University of Saskatchewan and has served in both academia and industry across Canada, Bangladesh, Malaysia, and Saudi Arabia. His expertise spans web-development, UVM-based SystemVerilog verification, static timing analysis (STA), RTL design, and scripting in multiple languages including Python, TCL, Shell as well as web-development tools including HTML, CSS, Javascript.
Passionate about knowledge sharing and education, Dr. Hasan has also worked as an Assistant Professor in Ontario, Canada (at Lakehead University) and Bangladesh University. He is committed to building accessible learning environments and is the founder of SkillSeminary, a platform focused on simplifying complex tech concepts for learners worldwide.
When he's not immersed in chip verification or educational projects, Dr. Hasan enjoys mentoring, researching system development, and promoting open tech education.