Mastering Pointer Arithmetic in C: Understanding the Basics of Pointer Operations


Pointer arithmetic is a technique used in C programming to manipulate memory addresses using mathematical operations. The basic arithmetic operations such as addition, subtraction, multiplication, and division can be performed on pointers to move to a specific memory location.

For example, you can add or subtract an integer value from a pointer to move a certain number of memory locations ahead or behind. Here is an example of pointer arithmetic in C:

  • The program starts by including the standard input-output library, stdio.h , which is used to input and output data.
  • In the main function, an integer variable a is declared and initialized with the value of 10. Two pointer variables p and r are also declared.
  • The pointer variable p is then assigned the memory address of the variable a using the address-of operator & .
  • The program then uses the sizeof operator to find the size of the int variable a
  • The pointer variable r is then assigned the result of p+1 . This is an example of pointer arithmetic where we are adding the value of 1 to the memory address stored in p
  • The program then uses the printf function to print the value stored in p and r
  • This program demonstrates how to use pointer arithmetic to move through memory locations and access the values stored at those locations. The program also demonstrates how to use the sizeof operator to find the size of a variable in bytes.

Source Code

#include<stdio.h>
int main()
{
    int a=10;
    int *p,*r;
    p=&a;
    r=p+1;
    printf("\nSize of Integer : %d",sizeof(a));
    printf("\nP Value         : %d",p);
    printf("\nR Value         : %d",r);
    return 0;
}
 
To download raw file Click Here

Output

Size of Integer : 4
P Value         : 6356724
R Value         : 6356728

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C