Basmah
Basmah

Reputation: 899

Which data structure to store addresses of objects in C++

I am beginner in C++, I need to know which data structure to store addresses of objects in C++.

Thanks

Upvotes: 1

Views: 2538

Answers (2)

bobobobo
bobobobo

Reputation: 67234

You would need to use something called a "pointer."

Normal variables, such as

int a = 5 ;
double r = 39.9 ;

Contain values your program should read and use.

Pointers are variables that don't contain values your program should read and use - instead, pointers contain the address of some variable your program will read and use.

For example:

int *pA ;
pA = &a ;      // pA is now a POINTER to a
*pA = 4 ;      // variable a now contains 4, not 5!

So in the above, a few things are happening. First, the pointer variable pA is declared using a * in its declaration.

int *pA ;

Next, we give pA a value. What value? Why the address of a!

pA = &a ;

The function of pA is like a secondary handle to the variable a. When you modify what pA points to, you are actually modifying the variable a now.

*pA = 4 ;

The variable pA points to at the moment (which is a) gets changed to 4.

See these videos for a great visualization.

Upvotes: 3

Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23789

A pointer. (and here's some SO padding :)

Upvotes: 2

Related Questions