-3

This is what I coded on Mobile C

#include <iostream>
using namespace std;

int main()
{
    int i{14},j{20};
    int *pj{&j},*pi{&i};
    
    cout<<endl<<(unsigned)pi;
    cout<<"\t"<<*pi;
    cout<<endl<<(unsigned)pj;
    cout<<"\t"<<*pj;
    
    --pi;
    ++pj;
    
    cout<<endl<<(unsigned)pi;
    cout<<"\t"<<*pi;
    cout<<endl<<(unsigned)pj;
    cout<<"\t"<<*pj;
    
    return 0;
}

And this was the output:

2582956840  14
2582956836  20
2582956836  20
2582956840  14

But when I'm trying to do this on visual code (fedora 36) and the terminal compiler g++ is throwing errors, as mentioned above in the Imgur image.

EDIT2:

I did this:

#include <iostream>
using namespace std;

int main()
{
    unsigned int i{14},j{20};
    unsigned int *pj{&j},*pi{&i};
    
    cout<<endl<<(unsigned*)pi;
    cout<<"\t"<<*pi;
    cout<<endl<<(unsigned*)pj;
    cout<<"\t"<<*pj;
    
    --pi;
    ++pj;
    
    cout<<endl<<(unsigned*)pi;
    cout<<"\t"<<*pi;
    cout<<endl<<(unsigned*)pj;
    cout<<"\t"<<*pj;
    
    return 0;
}

And got this output:

0x7fff539232d0  14
0x7fff539232d4  20
0x7fff539232cc  21863
0x7fff539232d8  1402090200

Strange! It was compiled using onlinegdb C++ compiler.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
SogaBan
  • 15
  • 5
  • 1
    [Please do not post images of code because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Code should be posted directly **as text** in your question. – MikeCAT Jul 13 '22 at 11:02
  • Post code&error(in text form), not image of code&error. – Jason Jul 13 '22 at 11:02
  • I think the error message describes exactly what you are doing wrong. Use `intptr_t` instead of `int` (also `#include `) – john Jul 13 '22 at 11:04
  • Your program has a bug that a modern compiler can easily detect. [Here](https://godbolt.org/z/e74ac7fad) is what you should have done before writing the first word of your question. – n. m. could be an AI Jul 14 '22 at 21:15

1 Answers1

0

You can get the pointers value by doing this:

#include <iostream>

int main() {
   int a = 2;

   cout < &a;
}

This is pretty much as best you can do to make it human readable. Also you can look at Displaying the address of a string if you want to look at any other approaches.

  • Please allow me to give some background. As I was following a UDEMY course video, the instructor was using the similar code to print out the hexadecimal address as a signed int value. And he was using codeblocks, though. I tried the same on my Android - "Mobile C" was the name of the app - and it worked - the way I intended. But when I tried the same on a laptop - I got those errors. – SogaBan Jul 13 '22 at 13:25
  • i think your cast is wrong (unsigned int) should be (signed int) or (int) please come back if this doesn't help and I'll have a deeper look. – Darrenthebozz Jul 13 '22 at 17:00
  • Made a mistake (signed int*) or (int*) should be used or remove the case entirely. Sorry. – Darrenthebozz Jul 13 '22 at 17:07
  • casting it to (signed int*) or (int*) for that matter - shows the hexadecimal value of the address - not exactly what I want. I have also tried using intptr_t (after including cstdint header) but I am getting the same error. – SogaBan Jul 14 '22 at 00:40
  • Isn't there any way to present the hexadecimal value of a memory address in the form of plain decimal in C++? Or is it something that cannot be done inherently? If such be the case, then how come the UDEMY video I came across showed it? – SogaBan Jul 14 '22 at 00:42
  • You want to convert a hexadecimal address to decimal? What you saw previously probably was hexadecimal but looked like a decimal. 0x10020982 is still hexidecimal even though there is no ABCDEF. you could try https://stackoverflow.com/questions/11031159/c-converting-hexadecimal-to-decimal. Still feel like im missing the point. – Darrenthebozz Jul 14 '22 at 10:01
  • You are getting one side of the picture (of my intention about the output of the code). Yes, I want to convert/typecast hex to decimal - but NOT for any number. I want to read the memory location/address in a 'decimal' way rather than the conventional 'hex' way. – SogaBan Jul 14 '22 at 11:03
  • this should work make sure to use uintptr_t or intptr_t as they need to be big enough or the number will be incorrect. – Darrenthebozz Jul 14 '22 at 11:28
  • unsigned a = 25; cout << (uintptr_t) &a << '\n'; cout << &a; – Darrenthebozz Jul 14 '22 at 11:28
  • int a = 25; cout << (intptr_t) &a << '\n'; cout << &a; – Darrenthebozz Jul 14 '22 at 11:39