0% found this document useful (0 votes)
5 views1 page

Pointer Arithmetic in C Example

Uploaded by

Sid1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Pointer Arithmetic in C Example

Uploaded by

Sid1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;

printf("Initial address stored in ptr: %p\n", ptr);


ptr++;
printf("After incrementing, ptr points to: %p, value: %d\n", ptr, *ptr);

ptr--; // Move pointer back to the previous element


printf("After decrementing, ptr points to: %p, value: %d\n", ptr, *ptr);

ptr += 2; // Move pointer forward by 2 elements


printf("After adding 2, ptr points to: %p, value: %d\n", ptr, *ptr);

ptr -= 2; // Move pointer backward by 2 elements


printf("After subtracting 2, ptr points to: %p, value: %d\n", ptr, *ptr);

// Arithmetic operations with pointers


int index = ptr - arr; // Calculate the index difference between ptr and arr
printf("Index difference between ptr and arr: %d\n", index);

return 0;
}

You might also like