C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Basics of Pointer Arithmetic
  • Increment and Decrement Operations
  • Pointer Arithmetic with Arrays
  • Pointer Arithmetic and Strings
  • Pointer Arithmetic and Dynamic Allocation

Was this helpful?

  1. Pointer

Pointer Arithmetic

Pointer arithmetic in C involves manipulating memory addresses using pointers, providing a powerful mechanism for navigating through arrays, strings, and data structures. This section explores the principles of pointer arithmetic and its applications in various scenarios.

Basics of Pointer Arithmetic

  • Pointer arithmetic allows you to perform arithmetic operations on pointers. When you add or subtract an integer value from a pointer, it moves to the memory location of the corresponding element in the array:

    int integerArray[5] = {10, 20, 30, 40, 50};
    int *pointerToArray = integerArray;
    
    // Move to the second element
    pointerToArray += 1;  // Now points to the element with value 20

Here, pointerToArray is incremented by 1, pointing to the second element in the array.

Increment and Decrement Operations

  • Incrementing and decrementing a pointer adjusts its address based on the size of the data type it points to:

    char charArray[3] = {'A', 'B', 'C'};
    char *pointerToChar = charArray;
    
    // Move to the next character
    pointerToChar++;  // Now points to 'B'

In this example, pointerToChar is incremented by 1, moving to the next character.

Pointer Arithmetic with Arrays

  • Pointer arithmetic is often used to traverse arrays:

    int integerArray[5] = {1, 2, 3, 4, 5};
    int *pointerToArray = integerArray;
    
    // Access the third element
    int thirdElement = *(pointerToArray + 2);  // Retrieves the value 3

The expression *(pointerToArray + 2) calculates the address of the third element.

Pointer Arithmetic and Strings

  • Pointer arithmetic is valuable when working with strings:

    char str[] = "Hello";
    char *ptr = str;
    
    // Access the second character
    char secondChar = *(ptr + 1);  // Retrieves 'e'

Here, ptr + 1 calculates the address of the second character in the string.

Pointer Arithmetic and Dynamic Allocation

  • Pointer arithmetic is often used with dynamically allocated memory:

    int *dynamicArray = (int *)malloc(3 * sizeof(int));
    
    // Move to the second element
    dynamicArray += 1;

When dealing with dynamically allocated memory, pointer arithmetic facilitates navigation through allocated blocks.

Mastering pointer arithmetic is essential for efficient memory manipulation and array traversal in C programming.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousUtilizing Pointers with FunctionsNextDynamic Memory Allocation

Was this helpful?