Apply for Zend Framework Certification Training

c



< 10 Advanced Coding Problems On String Pointers in C Passing pointers to functions and returning pointers from functions >



Pointer Arithmetic in C 
 
1. What is Pointer Arithmetic?
Pointer arithmetic means performing arithmetic operations on pointer variables.
Pointers store memory addresses, so arithmetic operations move the pointer to another memory location.
C allows the following operations on pointers:
Increment ptr++
Decrement ptr--
Addition ptr + n
Subtraction ptr - n
Difference between two pointers
The pointer moves according to the size of the data type it points to.
Example:
If a pointer points to an int (4 bytes):
ptr + 1 → moves 4 bytes ahead
ptr + 2 → moves 8 bytes ahead
2. Incrementing and Decrementing Pointers
Pointer Increment
When a pointer is incremented, it moves to the next memory location of its data type.
Example
#include<stdio.h>
int main(){
    int arr[3] = {10,20,30};
    int *ptr;
    ptr = arr;
    printf("Address: %p Value: %d\n", ptr, *ptr);
    ptr++;
    printf("Address: %p Value: %d\n", ptr, *ptr);
    return 0;
}
Output (Example)
Address: 1000 Value: 10
Address: 1004 Value: 20
 
Explanation:
ptr → arr[0]
ptr++ → arr[1]
Because int = 4 bytes.
 
Pointer Decrement
Pointer decrement moves the pointer back to the previous memory location.
Example
#include<stdio.h>
int main(){
    int arr[3] = {10,20,30};
    int *ptr = &arr[2];
    printf("Value: %d\n", *ptr);
    ptr--;
    printf("Value: %d\n", *ptr);
    return 0;
}
Output
Value: 30
Value: 20
3. Relationship Between Arrays and Pointers
 
In C, array names behave like pointers.
The array name represents the address of the first element.
arr == &arr[0]
Example:
#include<stdio.h>
int main(){
    int arr[3] = {10,20,30};
    printf("%d\n", arr[0]);
    printf("%d\n", *(arr+1));
    printf("%d\n", *(arr+2));
    return 0;
}
Output
10
20
30
Explanation:
arr + 1 → address of arr[1]
*(arr + 1) → value of arr[1]
 
Memory Representation
 
Address     Value
1000        10
1004        20
1008        30
arr = 1000
arr+1 = 1004
arr+2 = 1008
4. Pointer to an Array
A pointer to an array points to the entire array, not just a single element.
Syntax:
datatype (*ptr)[size];
Example:
#include<stdio.h>
int main(){
    int arr[3] = {10,20,30};
    int (*ptr)[3] = &arr;
    printf("%d\n", (*ptr)[0]);
    printf("%d\n", (*ptr)[1]);
    printf("%d\n", (*ptr)[2]);
    return 0;
}
 
Output
10
20
30
Explanation
ptr → whole array
(*ptr)[i] → access element
Memory View
ptr → arr → [10 20 30]
5. Array of Pointers
An array of pointers means an array where each element is a pointer.
Syntax
datatype *arr[size];
Example
#include<stdio.h>
int main(){
    int a=10,b=20,c=30;
    int *ptr[3];
    ptr[0] = &a;
    ptr[1] = &b;
    ptr[2] = &c;
    printf("%d\n", *ptr[0]);
    printf("%d\n", *ptr[1]);
    printf("%d\n", *ptr[2]);
    return 0;
}
Output
10
20
30
Memory Representation
ptr[0] → address of a
ptr[1] → address of b
ptr[2] → address of c
6. Difference: Pointer to Array vs Array of Pointers
Feature Pointer to Array Array of Pointers
Meaning Pointer to entire array Array containing pointers
Syntax int (*ptr)[5] int *ptr[5]
Access (*ptr)[i] *ptr[i]
Use Multidimensional arrays String arrays
 
Example:
Pointer to Array
int arr[5];
int (*ptr)[5] = &arr;
Array of Pointers
int *ptr[5];
7. Pointer Arithmetic with Arrays
Example:
#include<stdio.h>
int main(){
    int arr[5] = {1,2,3,4,5};
    int *ptr = arr;
    for(int i=0;i<5;i++)    {
        printf("%d ", *(ptr+i));
    }
    return 0;
}
Output
1 2 3 4 5
Explanation
ptr + i → next element address
*(ptr+i) → value
8. Important Rules of Pointer Arithmetic
? Only addition and subtraction allowed
? Cannot add two pointers
? Pointer arithmetic depends on data type size
? Works mainly with arrays
Example (Invalid)
ptr1 + ptr2  ?
Example (Valid)
ptr + 1  ?
ptr - 1  ?
9. Program: Traversing Array using Pointer Arithmetic
#include<stdio.h>
int main(){
    int arr[5]={5,10,15,20,25};
    int *ptr = arr;
    while(ptr < arr+5){
        printf("%d ", *ptr);
        ptr++;
    }
    return 0;
}
Output
5 10 15 20 25
Quick Summary
Concept Meaning
Pointer Arithmetic Arithmetic on memory addresses
ptr++ Move to next memory location
ptr-- Move to previous memory location
arr Address of first element
*(arr+i) Access array element
Pointer to array Pointer pointing to whole array
Array of pointers Array storing pointer variables

< 10 Advanced Coding Problems On String Pointers in C Passing pointers to functions and returning pointers from functions >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top