answersLogoWhite

0

What is pointer arithmetic in c with examples?

Updated: 8/20/2019
User Avatar

Wiki User

8y ago

Best Answer

Pointer arithmetic simply means performing arithmetic upon memory addresses. Memory addresses are fixed -- we cannot physically change them -- however we can store memory addresses in pointer variables and we can change those addresses using pointer arithmetic.

Just as an integer variable stores an integer value in memory, a pointer variable stores a memory address in memory. A memory address is itself an integer value, thus we can increment and decrement that memory address just as we can any other integer:

int i = 42;

++i;

int* p = &i;

++p;

When we increment an integer, that integer is increment by 1 unit, thus the value of i becomes 43. The value of p is also incremented by 1 unit, however that does not mean its address increments by 1 byte, it actually increments by 1 * sizeof (int) bytes. This is because p was declared a pointer to int. The compiler knows this and can therefore work out how many bytes to increment the pointer's value (the address stored in the pointer). In other words, p now points to the start address of the next int in memory.

Normally we use pointer arithmetic upon arrays:

int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int* p = x + 10;

Here, p is pointing at the element immediately after the last element of x (one element past the end of the array). This is because x + 10 increments the address of x by 10 elements of sizeof (int). Note that array names always implicitly decay to a pointer to the first element of the array, thus x + 10 is equivalent to writing &x[0] + 10.

Although we must never dereference memory that may not belong to us (as is the case in both examples), a pointer to the one-past-the-end of an array is useful because we can use pointer arithmetic to work out how many elements there are in a given sequence:

size_t length = p - &x[4];

Here, length will be 6 because there are 6 elements of sizeof (int) between the address of x[4] and the address stored in p. Note that we must subtract the lower address (&x[4]) from the higher address (p).

In C, arrays are always allocated with the first element (index 0) in the lowest address of the array and all higher indices in higher addresses. An element's index is itself an offset memory address from the start of the array, thus when we refer to x[4] we're actually referring to the element at address &x[0] + 4.

Just as we can increment memory addresses we can also decrement them. This is useful when we want to navigate to the next or previous element in an array:

p = p - 4;

Here, p is now referring to the element x[6].

Note that we can perform pointer arithmetic upon a pointer to any type except void. This is because a pointer to void can store the address of any type, but there's no way to determine that type's length.

void* v = (void*) x;

v += 2; // Error: sizeof (*v) is unknown!

Before we can perform pointer arithmetic upon a pointer to void, we must cast it to the correct type:

int* i = (int*) v;

i += 2;

v = (void*) i;

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is pointer arithmetic in c with examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


C program pointers to pointers examples?

Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;


What is pointer to a member in objective c?

It is a pointer that points to a member of a structure.


How can you offset a pointer in C?

Increment or decrement the pointer by the required offset.


Pointer arithemetic in C?

no


What is the purpose of pointer in C?

the purpose of pointer in c for saving the memory space.,and reduce the length and complexity of the program


What is an arithmetic operator in c?

+


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


What is the effect of various arithmetic operators on a pointers?

Error message, mainly. The following operations are legal: ptr + integer (pointer) ptr - integer (pointer) ptr - ptr (integer)


What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is stream pointer in c?

C does not have stream pointers.


What are the examples of arithmetic operation?

The 4 basic arithmetic operations are addition, subtraction, division and multiplication.