0

I have spent so much time on this I'm at the point of giving up. All I want to do is be able to show the pointer value of existCards on a Qlabel. I'm new at C++ and QT. I have read and watched many videos/tutorials and still can't grasp the concept. Any help will be appreciated.

typedef short  I16;

I16 existCards;
I16 CardNo=&existCards;


I16 _8134_initial(I16* existCards)
{
      return* existCards;
}


 MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_pushButton_clicked()
{

   QString strForLabel = QString("0x") + QString::number(existCards, 16);
   ui->label->setText(strForLabel);
}
Hank
  • 11
  • 8
  • the code is not doing what you ask for: the function `_8134_initial` is returning a I16 (the value not the pointer address) and you're displaying `CardNo` which again is not the address but rather the value of another `short` – bibi Feb 03 '16 at 13:27
  • Is `existCards` initialized? – vahancho Feb 03 '16 at 13:27
  • I've changed the QString conversion as per phyatt. However I'm getting error: C2440: 'initializing' : cannot convert from 'I16 *' to 'I16' There is no context in which this conversion is possible. Almost there. – Hank Feb 03 '16 at 14:16
  • Anywhere you are passing a pointer, you can't pass it the value, you need to pass a pointer, like `return ptr;` needs to have a type for the function of `int * func()` otherwise it says you are passing the wrong thing. Same with setting values. If you make a pointer you have to store it in a pointer. `I16* CardNo = &existCards;` (note the pointer after I16). – phyatt Feb 03 '16 at 14:19
  • Ok, I removed the CardNo decleration. Now when I run the program the output on the Label is 0x0. If I add I16* existsCards; as variable decleration, i get error: C2665: 'QString::number' : none of the 7 overloads could convert all the argument types If I add *existCards to the QString parameter, the app crashes – Hank Feb 03 '16 at 15:05
  • Here is some more info about pointers: [stanford info page](http://cslibrary.stanford.edu/106/) [SO explanation](http://stackoverflow.com/questions/7062853/c-pointer-assignment) [another link](http://faculty.ycp.edu/~dhovemey/spring2013/cs101/lecture/lecture15.html) All of your errors thus far, are related to misuse of pointers... These documents look like they have a good explanation. Good luck. – phyatt Feb 03 '16 at 16:27

1 Answers1

0

For printing out pointers, and debugging them, I would recommend using qDebug().

#include <QDebug>

//...

int* intPtr = new int(3);
qDebug() << "intPtr's address is" << intPtr;

For making a string out of it and using it in a label, you need to do a little bit of conversion.

QString strForLabel = QString("0x") + QString::number(intPtr, 16);
ui->label->setText(strForLabel);

Also another way to get a pointer from something on the stack is like so:

int myInt = 3;
int * intPtr = &myInt;

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80