avi
avi

Reputation: 21

Typecasting of Pointers in C++

Assume pointer size is 4

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

Output : 20

Here in this program why in typecasting it is printing the output as sizeof pointer? Why it's not printing 5?

Upvotes: 1

Views: 214

Answers (4)

Gopinath
Gopinath

Reputation: 4957

Each element of the array occupies 4 bytes of space in memory.

Therefore, the calculation can be done as follows:

  • Bytes between pointers = char_type_cast of (pointer2 - pointer1)
  • Pointer-sized spaces = (bytes_between_pointers / 4)
  • Integer-sized spaces = int_type_cast of (pointer2 - pointer1)

Working code:

#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    
    
    /* 
      1 pointer = 4 bytes 
  
      Byte-sized spaces = char_type_cast of (ptr2 - ptr1)
        
      Pointer-sized spaces = byte_spaces / 4
        
      Integer-sized spaces = int_type_cast of (ptr2 - ptr1)
    */
  
    int byteSpaces = ((char*)ptr2 - (char*)ptr1);
    int pointerSpaces = (byteSpaces / 4);
    int integerSpaces = ((int*)ptr2 - (int*)ptr1);
    
    
    printf("Pointer 1 = %d\n", *ptr1);
    printf("Pointer 2 = %d\n", *ptr2);
    printf("\nByte sized spaces between two pointers = %d\n", byteSpaces);
    printf("\nPointer sized spaces between pointers = %d\n", pointerSpaces);
    printf("\nInteger sized spaces between pointers = %d\n", integerSpaces);
    return 0;
}

Output:

Pointer 1 = 10
Pointer 2 = 60

Byte sized spaces between two pointers = 20

Pointer sized spaces between pointers = 5

Integer sized spaces between pointers = 5

Upvotes: 1

Tony Delroy
Tony Delroy

Reputation: 106236

When you do arithmetic on pointers, it operates in multiples of the pointed-to type's size. For that reason, the following assignments are equivalent:

int* ptr2 = arr + 5;
int* ptr2 = &arr[5];

Either way, it's a pointer to the 6th element in the array arr.

As the ints are each evidently 4 bytes in side, when you cast the pointers to char*s and subtract one from the other, the distance between the pointers is calculated in multiples of the new pointed-to type - char 's size, which is one. That difference between the 6th int and start of arr is 5 * sizeof(int) or 20 bytes.

For some background reading on pointers - consider my answer here.

Upvotes: 0

kesarling
kesarling

Reputation: 2260

Apart from @R sahu's answer, take a look at this code and run it through a debugger. You will find that t1 = '\n' and t2 = '<'. Subtraction is 20

Code:

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

Upvotes: 0

R Sahu
R Sahu

Reputation: 206717

Here in this program why in typecasting it is printing the output as sizeof pointer? Why it's not printing 5?

The cast to char* does that.

The offset between ptr2 and ptr1 is 5 as long as the pointers are of type int*. When you explicitly cast the pointers to char*, you are asking the program to compute the offset between the pointers as if they were char*. Your program says, it's 20, which is equal to 5*sizeof(int).

Upvotes: 1

Related Questions