The C++ Pointer


Discussing about pointer surely must known the reference. Both is a couple cannot be severanced.
  • & is the reference operator and can be read as “address of”
  • * is the dereference operator and can be read as “value pointed by”

This code is describing regarding pointer in C++ written in Qt

#include <QDebug>
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
int a = 10;
int* b = &a;
qDebug() << a << ” ” << b << ” “;
*b = 100;
int &c = *b;
qDebug() << a << ” ” << b << ” ” << c;
return app.exec();
}

Above written two output using qDebug().

in the first output

a: containing value 10 and own address

b: containing value address of a and own address

in the second output

a: containg value changed pointed by *b and own address not changed

b: containg value not changed, own address not changed, but changing the value of a

c: defined the own address as a adress and containg value pointed by *b

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: