Reputation: 13
I have recently started object-oriented software development in C++ and I am having a few issues trying to understand pointers. I am using Qt Creator 3.3.0.
1. Using (*type) To Store A New Address In The Variable
why I am always required to do:
int* y = ( int*) 0x61fe9c;
To store and address rather than:
int* y = 0x61fe9c;
Shouldn't the pointer understand that I am storing a hexadecimal address of a memory location ?
2. Storing A New Particular Address In A Pointer
I am trying to understand why once you store a particular address in a pointer you have to do it in a different way to store a new address:
int* y = ( int*) 0x61fe9c;
*y = 0x61fe9d;
If I do this the the address is stored without any problems but if I want to store it in this other way:
*y = (int*) 0x61fe9d;
The compiler gives me the error of invalid conversion from 'int*' to 'int'
.
Why can I not change the value of the address stored as shown above ?
Thank you in advance.
Upvotes: 0
Views: 1650
Reputation: 238351
why I am always required to do:
int* y = ( int*) 0x61fe9c;
To store and address rather than:
int* y = 0x61fe9c;
Because C++ has a strong type system.
The type system has been designed to prevent programmers from making mistakes, by unintentionally assigning a value of one type into an object of another type. Instead of allowing arbitrary things to happen at run-time, such programs are ill-formed (except for example when one type is implicitly convertible to the other type; this is not such case).
In this case, you're attempting to assign an integer to a pointer. Such operation is quite unusual, and you must explicitly convert the value of integer type into a pointer pointer type.
I am trying to understand why once you store a particular address in a pointer you have to do it in a different way to store a new address:
*y = 0x61fe9d;
This doesn't store an address in the pointer i.e. this doesn't make the pointer point to another memory location. You indirect through the pointer using the indirection operator (the unary *
), so the value of the expression is the pointed object, not the pointer itself.
To change the value of the pointer, don't indirect through it:
y = address_of_another_object;
*y = (int*) 0x61fe9d;
The compiler gives me the error of
invalid conversion from 'int*' to
'int'.
Just like above the type system prevents you from assigning value of one type into an object of another type, except in the other direction. In this case you attempt to assign a pointer value into an integer object.
Since your intention appears to not have been to modify the pointed object, it appears that the type system saved you from your mistake.
P.S. The behaviour of accessing the memory by indirecting through the pointer is undefined, unless you've created an object there. And you cannot create an object in arbitrary memory location unless you've allocated the memory. And there is no standard way of allocating memory at an arbitrary location.
The only situation where indirecting through an integer literal converted to a pointer makes sense that I know of is some embedded systems where documented addresses communicate with the hardware. In such case, you should probably need to qualify the pointed type as volatile
.
Upvotes: 2
Reputation: 10184
If I do this the the address is stored without any problems
*y = 0x61fe9d;
just stores the value 6422173
(decimal representation of 0x61fe9d) in an address pointed to by y.
The compiler gives me the error of invalid conversion from 'int*' to 'int'.
*y = (int*) 0x61fe9d;
gives an error because you are trying to store a pointer variable to an int type.
Why can I not change the value of the address stored as shown above ?
Just do y = (int*) 0x61fe9d;
Or per @user463035818 and @Quentin's comment, as a better practice, use reinterpret_cast
to get compile time safety.
y = reinterpret_cast<int*>(0x61fe9d);
Upvotes: 1