C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we will learn about the operations on pointer variables, including addition, subtraction, and comparison operations in the C programming language. Performing arithmetic operations on pointers is particularly useful when working with arrays and dynamic memory.
Pointer arithmetic:
C allows you to perform arithmetic operations on pointers, such as incrementing or decrementing them to access adjacent memory locations.
ptr++
or ++ptr
):Incrementing a pointer moves it to the next memory location based on the data type of the pointer. For example, if ptr
is an integer pointer, incrementing ptr
will move it to the next integer memory location (usually 4 bytes ahead on most systems).
int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; // Pointing to the first element of the array ptr++; // Move to the next element
ptr--
or --ptr
):Decrementing a pointer moves it to the previous memory location based on the data type of the pointer.
int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr + 4; // Pointing to the last element of the array ptr--; // Move to the previous element
ptr + n
or n + ptr
):You can add an integer to a pointer to move it forward by n
memory locations of the pointer's data type.
int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; // Pointing to the first element of the array ptr = ptr + 3; // Move 3 elements forward
ptr - n
):You can subtract an integer from a pointer to move it backward by n
memory locations of the pointer's data type.
int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr + 4; // Pointing to the last element of the array ptr = ptr - 2; // Move 2 elements backward
ptr1 - ptr2
):You can subtract one pointer from another to find the distance between them in terms of the number of elements of the pointer's data type.
int arr[] = {1, 2, 3, 4, 5}; int *ptr1 = arr; int *ptr2 = arr + 3; int distance = ptr2 - ptr1; // distance = 3
Pointer comparison:
You can compare pointers using the relational operators, such as ==
, !=
, <
, >
, <=
, and >=
. Pointer comparison is useful when working with arrays, as it allows you to check if a pointer has reached the end of an array or is within a valid range.
int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; int *end = arr + 5; if (ptr < end) { printf("Pointer is within the array range.\n"); } else { printf("Pointer is outside the array range.\n"); }
In summary, performing operations on pointer variables in C programming language, such as addition, subtraction, and comparison, allows you to efficiently traverse and manipulate arrays and other data structures.
Adding and Subtracting Values from Pointer Variables in C:
Perform arithmetic operations on pointers to navigate through memory.
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr = numbers; printf("First element: %d\n", *ptr); ptr = ptr + 2; // Move two elements forward printf("Third element: %d\n", *ptr); return 0; }
Output:
First element: 1 Third element: 3
Adding an integer to a pointer increments it by the size of the pointed type.
Comparison of Pointer Variables in C Language:
Compare pointers for ordering or equality.
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr1 = &numbers[2]; int *ptr2 = &numbers[4]; if (ptr1 < ptr2) { printf("ptr1 comes before ptr2\n"); } return 0; }
Output:
ptr1 comes before ptr2
Comparing pointers compares their memory addresses.
Pointer Arithmetic and Array Traversal in C Programming:
Use pointer arithmetic for efficient array traversal.
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr = numbers; for (int i = 0; i < 5; ++i) { printf("Element %d: %d\n", i + 1, *ptr); ptr++; } return 0; }
Output:
Element 1: 1 Element 2: 2 Element 3: 3 Element 4: 4 Element 5: 5
Incrementing the pointer traverses the array.
C Code Examples Demonstrating Pointer Operations:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr = numbers; printf("First element: %d\n", *ptr); ptr++; printf("Second element: %d\n", *ptr); return 0; }
Output:
First element: 1 Second element: 2
Pointers allow efficient access and manipulation of data.
Limitations and Precautions with Pointer Arithmetic in C:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr = &numbers[4]; // Avoid going beyond the array bounds // ptr = ptr + 2; // Error: undefined behavior printf("Fifth element: %d\n", *ptr); return 0; }
Out of bounds pointer arithmetic results in undefined behavior.
Pointer Arithmetic with Different Data Types in C:
#include <stdio.h> int main() { double values[] = {1.1, 2.2, 3.3, 4.4}; double *ptr = values; printf("First element: %.1f\n", *ptr); ptr++; printf("Second element: %.1f\n", *ptr); return 0; }
Output:
First element: 1.1 Second element: 2.2
Pointer arithmetic adjusts based on the size of the pointed type.
Comparing Pointers for Equality and Inequality in C:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int *ptr1 = &numbers[2]; int *ptr2 = &numbers[2]; if (ptr1 == ptr2) { printf("Pointers are equal\n"); } return 0; }
Output:
Pointers are equal
Compare pointers using equality or inequality operators.