huskywolf
huskywolf

Reputation: 525

Casting from int* to char* in c++

I understand these lines but why does

?

Should it not print 11 instead of 20 since line 1 and 2 print 'X' and 'c'.

Your help is much appreciated.

#include "iostream"
using namespace std;
int main()
{
int arr[] = {88, 20, 30, 40, 50, 99};
int *ptr1 = arr;
int *ptr2 = arr + 5;
cout<<(char*)ptr1<<endl;//line 1
cout<<(char*)ptr2<<endl;//line2
cout<<('c'-'X')<<endl;//line3
cout<<ptr2-ptr1<<endl;//line4
cout<<(char*)ptr2-(char*)ptr1<<endl;//line5

return 0;
}

Upvotes: 3

Views: 931

Answers (5)

Milan
Milan

Reputation: 141

The first ptr1 points to these bytes in memory: 88,0,0,0 - which is the first index in array.
The ptr2 points to these bytes in memory: 99,0,0,0.
So the first two lines are outputting two null terminated strings: X and c.

The third line is outputting the difference between chars c and X, which is int value.

The fourth value is difference between two pointers in respect to on what these pointers are pointing to (relative position in array). This is an int number.

The last is cast to char*, where char is 1 byte, which makes it 4 times smaller then int (int is 4 bytes big). So the difference is (array position difference) * (size of int) = 5 * 4 = 20. This value is int so that is why you see number 20.

Try to change all those ints in to char and see the difference:

char arr[] = { 88, 20, 30, 40, 50, 99 };
char *ptr1 = arr;
char *ptr2 = arr + 5;

Suddenly these first two lines are no longer one chars, because there is no longer the 0 terminator for (char*) strings.

X¶▲(2c╠╠╠╠╠╠╬H@▼ñ¸4
c╠╠╠╠╠╠╬H@▼ñ¸4
11
5
5

This will vary between debug and release.

Upvotes: 0

LPs
LPs

Reputation: 16243

Because of (char*)ptr2-(char*)ptr1 is the difference of addresses. Then 5*sizeof(int) = 20 in your (I guess) 32 bits platform.

Upvotes: 1

Vineet1982
Vineet1982

Reputation: 7918

The answer is absolutely correct let me explain your anwer step by step according to code:

cout<<(char*)ptr1<<endl;//line1

Since ptr1 = arr, therefore first char is 88 and thus printed X

cout<<(char*)ptr2<<endl;//line2

Since ptr1 = arr +5, therefore fifth char is 99 and thus printed c

cout<<('c'-'X')<<endl;//line3

Since the value of 'c' =99 and 'X' = 88 thus answer is 11

cout<<ptr2-ptr1<<endl;//line4

Since the arr is an array and distance between 5th and 0th element 5-0 = 5

cout<<(char*)ptr2-(char*)ptr1<<endl;//line5

What here happens there you are subtracting two address and answer is provided as differance of two addresss, for instance

 (char *)ptr1 --- Address = 10
 (char *)ptr2 --- Address = 30

and if we subtract both then 30-10=20. Why it is 20 as it depends on the sizeof void *

32 bit sizeof void* = 4
64 bit sizeof void* = 8

Thus on 32 bit system answer would be 4*5=20 and on 64 bit system it would be 8*5=40

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

In the environment where the program was compiled sizeof( int ) is equal to 4. That is each object of type int occupies 4 bytes (characters). Between ptr2 and ptr1 there are 5 integers. The difference of two pointers that point to elements of the same array is the number of elements between them. It is so-called pointer arithmetic. Thus ptr2 - ptr1 gives 5. But if you will cast these pointers to type char * then between these pointers there are 5 * sizeof( int ) characters that is equal to 20.

Upvotes: 4

Vagish
Vagish

Reputation: 2547

TO get the output what you are expecting use:

(char)*ptr2-(char)*ptr1

Upvotes: 1

Related Questions