I'm trying to store the value of an address in a non pointer int variable, when I try to convert it I get the compile error "invalid conversion from 'int*' to 'int'" this is the code I'm using:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
vector<int> test;
int main() {
    int *ip;
    int pointervalue = 50;
    int thatvalue = 1;
    ip = &pointervalue;
    thatvalue = ip;
    cout << ip << endl;
    test.push_back(thatvalue);
    cout << test[0] << endl;
    return 0;
}
                Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined.
The "address of" operator This is the best way to attach a pointer to an existing variable: int * ptr; // a pointer int num; // an integer ptr = # // assign the address of num into the pointer // now ptr points to "num"!
You can cast a pointer to another pointer of the same IBM® i pointer type. Note: If the ILE C compiler detects a type mismatch in an expression, a compile time error occurs. An open (void) pointer can hold a pointer of any type.
int may not be large enough to store a pointer.
You should be using intptr_t. This is an integer type that is explicitly large enough to hold any pointer.
    intptr_t thatvalue = 1;
    // stuff
    thatvalue = reinterpret_cast<intptr_t>(ip);
                // Convert it as a bit pattern.
                // It is valid and converting it back to a pointer is also OK
                // But if you modify it all bets are off (you need to be very careful).
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With