DEV Community

CharmingAkashRaj
CharmingAkashRaj

Posted on

I am getting an error while compiling the c++ code, please help me out to fix this.

Alt Text

You can go through https://onlinegdb.com/Hy_FYU9JP to resolve the bug

Top comments (5)

Collapse
 
dwd profile image
Dave Cridland

Waleed has the details, but you know you needn't cast this at all?

#include<iostream>
// #include<cstdlib>
/* You don't need that. */
using namespace std;

int main()
{
    int temp = 300;

    cout << "Address of variable temp:  " << &temp << std::endl;
    return 0;
}

That compiles fine, and will work, printing out the pointer address in Hex, as is normal.

As Waleed says, if you want it as an integer (and printed in traditional decimal), you need the integer type to be big enough. There's a std::intptr_t for this:

#include<iostream>
using namespace std;

int main()
{
    int temp = 300;

    cout << "Address of variable temp:  " << (std::intptr_t)&temp;
    return 0;
}
Collapse
 
charmingakashraj profile image
CharmingAkashRaj

thanks, I got that 😊

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
charmingakashraj profile image
CharmingAkashRaj • Edited

I just want to change the output as an integer and I am unable to debug this error, please help me out.

dev-to-uploads.s3.amazonaws.com/i/...

Collapse
 
charmingakashraj profile image
CharmingAkashRaj

I am getting output in hexadecimal without using an unsigned keyword but , I just want to change output as an integer, can you tell me how to do this, and yes, my OS is a 64 bit.