- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Pointer Arithmetic
![]() Share with a Friend |
C Programming - C Pointer Arithmetic
C Pointer Arithmetic
Pointer arithmetic allows you to perform operations on pointers in C, such as incrementing, decrementing, and calculating the difference between pointers. Pointer arithmetic is primarily used to traverse arrays or perform operations on memory addresses directly.
Pointer Arithmetic Operations
- Incrementing a Pointer (++)
- When you increment a pointer, it moves to the next memory location according to the size of the data type it is pointing to.
Example:
C
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("First element: %d\n", *ptr); // 10
ptr++; // Move to the next element
printf("Second element: %d\n", *ptr); // 20
- Decrementing a Pointer (--)
- When you decrement a pointer, it moves to the previous memory location according to the size of the data type it is pointing to.
Example:
C
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr + 3; // Point to the 4th element (40)
printf("Current element: %d\n", *ptr); // 40
ptr--; // Move to the previous element
printf("Previous element: %d\n", *ptr); // 30
- Pointer Difference
- You can subtract two pointers pointing to elements of the same array. The result is the number of elements between them.
Example:
C
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[1]; // Points to 20
int *ptr2 = &arr[4]; // Points to 50
printf("Difference: %ld\n", ptr2 - ptr1); // 3 (since there are 3 elements between 20 and 50)
- Pointer Addition
- You can add an integer to a pointer. This adds the integer times the size of the data type to the pointer.
Example:
C
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
ptr = ptr + 2; // Move to the 3rd element (30)
printf("Third element: %d\n", *ptr); // 30
Important Points About Pointer Arithmetic:
- Pointer Type Matters:
- Pointer arithmetic depends on the size of the data type. When you increment or decrement a pointer, the pointer moves by the size of the data type it points to. For example, an int pointer will move by 4 bytes (on most systems), and a char pointer will move by 1 byte.
Example:
C
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
ptr++; // Moves 4 bytes ahead (next integer)
printf("Next integer: %d\n", *ptr); // 20
- Array and Pointer Relationship:
- In C, the name of an array is essentially a pointer to its first element. Thus, pointers and arrays are closely related, and pointer arithmetic allows you to navigate through the array elements.
Example:
C
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
printf("First element: %d\n", *ptr); // 10
ptr++; // Move to the next element
printf("Second element: %d\n", *ptr); // 20
- Pointers and Memory Allocation:
- Pointer arithmetic is often used when working with dynamic memory allocation (e.g., with malloc or calloc). It allows for efficient memory manipulation.
Example of Pointer Arithmetic:
C
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
// Print elements using pointer arithmetic
printf("Using pointer arithmetic:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, *(ptr + i));
}
// Pointer difference
int *ptr1 = &arr[1]; // Points to 2
int *ptr2 = &arr[4]; // Points to 5
printf("\nDifference between ptr1 and ptr2: %ld\n", ptr2 - ptr1); // 3 (3 elements between 2 and 5)
return 0;
}
Output:
Using pointer arithmetic:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Difference between ptr1 and ptr2: 3
Conclusion:
Pointer arithmetic is a useful feature in C that allows you to work directly with memory and manipulate data efficiently. It is most often used when dealing with arrays, dynamic memory, and linked lists. However, it requires careful handling since improper pointer arithmetic can lead to undefined behavior, such as accessing memory out of bounds or causing memory leaks.
