+ 1
If you can add up the value of variable a pointer points to like i did in the code , then why can't you add the addresses
14 Respuestas
+ 3
Well actually I also couldn't understand why it's giving 1 because if you print the addresses of 'a' and 'b' you will see a difference of only 4 in hexadecimal and also in decimal.
I don't understand why it's giving 1
+ 3
Anna I think you are correct, 1 is representing one integer variable
+ 2
Don't wrap it up so quickly!
First and foremost, getting the difference between two addresses is not something terribly strange. It's used with arrays quite a lot sometimes. But a careful programmer must bare in mind several facets before jumping blindly into the realm of undefined behaviour.
#1: Taking the difference of two values that are not in the same container (like array) yields undefined behavior.
#2: When you summoned up the courage to do the stuff use ptrdiff_t ¹
type to hold the difference.
#3: Take the absolute value from the result. Because you don't know which address comes before the other.
#4: The result is depends on the type of two pointers. That is, the result of subtraction of two char pointers are totally different.
#5: The result is a multiply of size of pointer's type. In above example, 1 means 1 * sizeof(int) which in turn tells us that the compiler has been padded 1 integer size between two consecutive addresses.
_____
¹
https://en.cppreference.com/w/cpp/types/ptrdiff_t
+ 1
Because pointers aren't designed to do that and to be honest it doesn't really make sense to add addresses. You can add an offset to an address though
+ 1
Of course you can add hexadecimal numbers. But why would you want to add addresses? You wouldn't even know what would be in that address. It's like randomly adding two house numbers. However you can subtract pointers to get the 'distance' between them (the result will be a number of bytes though, not an address). Same with house numbers: if you subtract them, you'll at least have an idea how far the houses are apart (theoretically). So that kind of makes sense
+ 1
yeah you are exactly riht
+ 1
thank you so much for helping me
0
ok yeah logically addressess aren't meant to be added up
but maybe you can add up hexadecimals ??
0
1)what do you mean by binary values . does it mean numbers that are converted into binary
2) do you substract the pointers (to get addresses) by using the ' - ' operator
0
so i did just that and it gave me the value 1 . Does that mean the distance between variables a and b is 1 byte ?
0
I'd rather say that it shows that a and b are in consecutive memory addresses. If you add another int c, the distance to a will probably be 2
0
I agree that the result of the subtraction of b and a should actually be 4. It's difficult to explain and I don't know if I'm 100% correct, but my theory is that C++ is smart enough to know that if you subtract two pointers, you don't really want to subtract two hexadecimal numbers, but you want to get a value that you can actually use for something. In this case, 1 means that you can store one variable of the defined type between the two memory addresses (4 bytes = 1 int).
0
oohh okkkk . Thanks