- 5
Pointers
I can't understand what pointers mean
5 Réponses
+ 6
Every variable lies at a certain location in memory, like a book lies at a certain location in a shelf. Now imagine you want to sort the books in the shelve by genre. After that you want to sort them by author. Now you could go ahead and actually do that: Rerange every book in the shelf. But that's alot of work.
You could also write each books details (author, genre) and its location in the shelf (its address) on small cards. Now you just need to sort the cards which you can do much easier and faster.
A pointer in C++ is just that: A variable that contains an address of another variable.
In C++ (and C, where pointers are much more common), objects can get quite large and difficult to move around. Also, you often need to sort and re-sort data. Moving around the actual data in memory is costly. Also, sometimes you may not own an object: you got it from a different part of your program or from a library or another program. I those cases you'll want to use pointers. For the C++ STL the various iterators provide the same functionality as raw pointers.
+ 5
In c++, a pointer takes the address location of the variable that it points to.
For example, if you want to point to an int variable, you can do so such as:
Int num = 5;
Int* ptr = #
The '*' asterisk symbole means its a pointer, and the '&' ampersand sign is the address of operator.
- 1
Fill in the blanks to declare a pointer to the num variable and double its value via the pointer.
int num = 42;
int
ptr =
num;
ptr *= 2;
- 4
thanks
- 5
Hi