0

Good day all

I am familiarizing myself with pointers and references,

apologies if the question contians more info than required, but it might be helpful to others

Purpose:

I pass the memory address of test_string as &test_string to a constructor, the constructor definition accepts a memory address using QString *ptr_test as a parameter, and assigning this in the new class to a pointer location defined by QString *pointer = ptr_test.

This, essentially, is a pass-by-reference method, but just using memory addresses all the way.

Question:

Now I have a memory address in pointer, what method can I use to display the value at the address and/or assign this to e.g. QString pointerValue where pointerValue will contain the value of the memory address specified in pointer

For information:

Calling Code

QString test_string = "This string is created in loginwindow and pointer sent to dialog";
qDebug() << "Displaying orignial variable";
qDebug() << "Displaying test_string = " << test_string;
qDebug() << "Displaying &test_string = " << &test_string;
qDebug() << "/nDisplaying tests:";
...
//code inbetween
...
LoginStatusDialog *dlgConnectStatus = new LoginStatusDialog(test_string, test_string, test_string, &test_string);
dlgConnectStatus->exec();

Header

public:
    LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent = 0);
    ~LoginStatusDialog();

private:

    Ui::LoginStatusDialog *ui;
    QString login, key, auth_tok, *pointer;

Implementation

LoginStatusDialog::LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent) :
    QDialog(parent), ui(new Ui::LoginStatusDialog), login(_login), key(_key), auth_tok(_auth_tok), pointer(ptr_test)
{
    qDebug() << "Passed with const &_login";
    qDebug() << "Displaying login = " << login;
    qDebug() << "Displaying _login = " << _login;
    qDebug() << "Displaying &login = " << &login;
    qDebug() << "Displaying &_login = " << &_login;

    qDebug() << "\nPassed with const _auth_tok";
    qDebug() << "Displaying auth_tok = " << auth_tok;
    qDebug() << "Displaying _auth_tok = " << _auth_tok;
    qDebug() << "Displaying &auth_tok = " << &auth_tok;
    qDebug() << "Displaying &_auth_tok = " << &_auth_tok;

    qDebug() << "\nPassed address with const _key";
    qDebug() << "Displaying key = " << key;
    qDebug() << "Displaying _key = " << _key;
    qDebug() << "Displaying &key = " << &key;
    qDebug() << "Displaying &_key = " << &_key;

    qDebug() << "\nPassed with *ptr_test";
    qDebug() << "Displaying test = " << pointer;
    qDebug() << "Displaying ptr_test = " << ptr_test;
    qDebug() << "Displaying &test = " << &pointer;
    qDebug() << "Displaying &ptr_test = " << &ptr_test;

    QString *pointerValue = ptr_test;
    qDebug() << "Display &pointerValue = ptr_test : " << pointerValue;

Output

Displaying orignial variable
Displaying test_string =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &test_string =  0x7fffffffd330

Displaying tests:
Passed with const &_string_
Displaying login =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _login =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &login =  0x5555559a4550
Displaying &_login =  0x7fffffffd330

Passed with const _string_
Displaying auth_tok =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _auth_tok =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &auth_tok =  0x5555559a4560
Displaying &_auth_tok =  0x7fffffffd4c0

Passed address with const _string_
Displaying key =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _key =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &key =  0x5555559a4558
Displaying &_key =  0x7fffffffd4b0

Passed with *ptr_string_
Displaying test =  0x7fffffffd330
Displaying ptr_test =  0x7fffffffd330
Displaying &test =  0x5555559a4568
Displaying &ptr_test =  0x7fffffffcf98

Display &pointerValue = ptr_test :  0x7fffffffd330
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • 1
    what do you expect the value is? At the pointer address is probably some "cryptic" things which have the memory of the object. – Hayt Oct 10 '16 at 09:09
  • But here you have some detailed things about pointers and how to work with them: http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean – Hayt Oct 10 '16 at 09:11
  • 1
    `QString` does not contains value itself. So it is unclear what you want to do. `QString` uses `QSharedData` inside to keep text. And it implements COW, so it is unsafe to keep raw pointers to inner text. – Dmitry Sazonov Oct 10 '16 at 09:32
  • @Hayt thanks for the assist, see my answer – CybeX Oct 10 '16 at 09:57

1 Answers1

0

I have found my solution (if one would call it that).

I need to apply a technique called

dereferencing link given by @Hayt - see comment on question

although the word "meaning/sound" does not correlate to what it actually does. See links provided.

Initially I had the following

QString *pointerValue = ptr_test;
qDebug() << "Display *pointerValue = ptr_test : " << pointerValue;

    //Output : Display pointerValue = ptr_test :  0x7fffffffd330    <--- the value contained by '*pointer' which is 0x7fffffffd330
    //note that at address '0x7fffffffd330', the value contained there is the string "This string is created in loginwindow and pointer sent to dialog"

qDebug() << "Display &pointerValue = ptr_test : " << &pointerValue;

    //Display &pointerValue = ptr_test :  0x7fffffffd270            <---the address of '*pointer'

But thanks to this post I came across, for easy reading purposes I am quoting the portion that assisted me in solving the issue:

The next example demonstrates the correct usage of pointers:

#include using namespace std;

void main()
{
  int x;
  int *ptr_p;

  x = 5;
  ptr_p = &x;

  cout << *ptr_p;          < --- note the * to refer to the memory location: added by @KGCybeX
}

Note: If you forget to place * (in front of the pointer) in the cout statement, you will print the address of integer x. (Try it).

The Solution/Answer

Thus my solution for display a pointer containing the memory address of another variable is:

qDebug() << "Display *pointerValue = ptr_test : " << *pointerValue;
//Display *pointerValue = ptr_test :  "This string is created in loginwindow and pointer sent to dialog"
Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115