简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM