PDA

View Full Version : display pointer memory address



milosav
4th June 2009, 01:44
First post from a qt newbie.
How can I achieve this in qt4. What header
files would I need to include? If possible a simple code example would be really appreciated.

int variable = 2;
int* pointer = &variable;

cout<<*pointer<<endl; //displays 2
cout<<pointer<<endl; //displays the location of "variable"

Thanks in advance.
Milo

nish
4th June 2009, 03:07
#include <QApplication>
#include <QLabel>

int main(int argc, char** argv)
{
QApplication app(argc,argv);

QLabel label;

int variable = 2;
int* pointer = &variable;

QString num=QString::number(variable);
QString p=QString::number(pointer,16);

label.setText("number="+num+"pointer="+p);
label.show();

return app.exec();

}

milosav
4th June 2009, 05:43
Mr Death,
Thank you for the fast reply.
Unfortunately the build finishes with the following error.

error: call of overloaded ‘number(int*&, int)’ is ambiguous

Build complains that QString does not seem to have a candidate number() function

This is the same issue that led me to posting. I was unable to find a Qt function to solve the issue and figured I must be missing something simple.

Any ideas ??

aamer4yu
4th June 2009, 06:34
I guess its getting confused in the pointer address
try it with double conversion -
QString p=QString::number((double)pointer,16);

milosav
4th June 2009, 07:55
Same casting issue it would seem.

Build Error
error: invalid cast from type ‘int*’ to type ‘double’

This should be really straight forward right?

faldzip
4th June 2009, 08:22
cast it to int:


String p=QString::number((int)pointer,16);
this works for me;

nish
4th June 2009, 08:23
i have tested it... it works for int instead of double
QString p=QString::number((int)pointer,16);

edit: i was a lil late in reply... did not saw the above post..:p:D
XX

milosav
5th June 2009, 00:56
Thanks a heap for your replies and your solution faldżip of casting to (int).

hdunn
2nd February 2016, 09:54
Still useful after all these years. Thanks.