How exactly does pointer subtraction work in the case of an integer array?

#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    printf("Number of elements between two pointer are: %d.", 
                                (ptr2 - ptr1));
    printf("Number of bytes between two pointers are: %d",  
                              (char*)ptr2 - (char*) ptr1);
    return 0;
}

      

For the first statement, the printf()

output will be 5

according to the pointer subtraction confusion

What about the second operator printf()

, what's the output?

+3


source to share


4 answers


To quote the C11

chapter ยง6.5.6, Additive Operators

When two pointers are subtracted, both must point to elements of the same array object, or one after the last element of the array object; the result is the difference between the indices of the two array elements.

So when you do

printf("Number of elements between two pointer are: %d.", 
                            (ptr2 - ptr1));

      

two ptr1

and ptr2

are pointers to int

, so they give the difference in the index 5. In other words, the difference between the calculated address relative to sizeof(<type>)

.



OTOH,

 printf("Number of bytes between two pointers are: %d",  
                          (char*)ptr2 - (char*) ptr1);

      

Both ptr1

and ptr2

written to the pointer to char

the size of which is 1 byte. The corresponding calculation is performed. Result: 20

.

FWIW , note that subtracting two pointers produces the result as a type ptrdiff_t

, and you must use a format specifier %td

to print the result.

+9


source


If you have two type pointers T

that point to elements of the same array, then the difference in pointers gives the number of type elements T

between those pointers

So the first statement of the output

printf("Number of elements between two pointer are: %d.", 
                            (ptr2 - ptr1));

      



outputs 5 - number of type elements int

between pointers ptr1

and ptr2

.

This is the so-called pointer arithmetic.

Pointers (char*)ptr1

and (char*)ptr2

have the same meanings as the original pointers ptr1

and ptr2

, but they treat (reinterpret) the amount of memory as an array of type char

, each element of which has a size equal to sizeof( char )

. In C, sizeof( char )

it is always 1. Thus, the difference ( char * )ptr2 - ( char * ) ptr1

gives the number of elements of the type char

that can correspond to the memory size. Obviously sizeof( char )

no more sizeof( int )

. Thus, there can be more elements of a type in the same memory char

than of a type int

. If, for example, sizeof( int )

is equal 4

, then the amount of memory can contain 5 * sizeof( int )

elements of the type char

that 20

.

+5


source


Pointer arithmetic is always in base type units.

In your case, you have

int *ptr1 = ...

      

Then execution ptr1 + 5

will add sizeof(*ptr1) * 5

bytes to the pointer ptr1

. Given that sizeof(*ptr1)

(same as sizeof(int)

) 4

, you get 4 * 5

which is equal to 20

bytes.

+4


source


Each element of the array is int

, and there are elements between the two pointers 5

. So there will be a 5*sizeof(int)

number of bytes between the two pointers .

+4


source







All Articles