Question:
Why are pointers useful in C++?
Anwar
2012-05-05 18:26:47 UTC
beginner programmer, and i don't see how pointers can be useful, if i can directly call a variable with a value. Why would anyone even need to know the memory address? Does this make the compiling process faster?
Five answers:
Cubbi
2012-05-06 16:03:36 UTC
Pointers are useful for several reasons, none of which has anything to do with memory addresses.



The top five, in my opinion, are:



1) dynamic storage duration

Whenever you use the keyword "new", you get a pointer to the object that was created. That pointer is the only handle you have to keep track of that object, to access it, and, ultimately, to end its life.



2) Aggregation

Aggregation is an OOP relationship: it's when an object knows about, keeps track of several other objects, which exist elsewhere, e.g. are not just parts of the first object (that would be composition).



3) Shared ownership

This is another OOP concept: objects may share ownership of another object: the owned object needs to exist as long as any of its owners exist, but disappear when the last owner is gone.



4) polymorphic containers

Whenever you need to store a number of different objects that are all derived from the same base class, you can create a container of pointers to base, each set to point at one of those objects.



5) Rebindable references

Imagine you're implementing a balanced tree data structure: each node needs to refer to its descendants its parent, but it also needs to be able to switch these links whenever the tree is rebalanced. Such switchable references are exactly pointers.
JB
2012-05-05 19:07:37 UTC
Most of the times when programmers work with data, they pass around references to data rather than the data itself.



Here is an analogy:



Just like a home address that maps a number, street, etc to a house, think of computer addresses as a number mapping data. Rather than visit a home and place mail in the mailbox, we can put mail in the mailbox by simply knowing it's address.



Sometimes this



int number = 5;



int main()

{

changeNumber(&number);

}



void changeNumber(int * number)

{

(*number) = 8;

}



is easier than this



int number = 5;



int main()

{

number = changeNumber(number);

}



int changeNumber(int number)

{

number = 8;

return number;

}



This is especially true as you get into more advanced programming and you start to work with more "stuff". There are other uses for pointers that you will learn about.
AirMc
2012-05-05 21:45:10 UTC
You are a beginner, and until you do some oop, or some intermediate c++ programming you cannot see the usefulness of pointers. The reality is if you can live without pointers that is perfectly fine.

So the answer to your question is wait and see!
2017-03-03 19:25:31 UTC
A pointer is exceptionally much continuously 4 bytes, whilst the form pointed to could desire to be interior the 1000's of bytes (extra, in case you think approximately that concepts often element to extensive linked lists). whilst a parameter is surpassed to a function, that is added to a fastened-length section stated as the Stack. putting a pointer onto the stack is plenty extra efficient, the two timewise and area smart, than putting a shape, extremely once you think approximately that a function can call yet another function, which additionally has to apply the stack. 2nd, handing off a linked checklist that is scattered over memory is impossible shop via using a pointer to it. In different words, handing a pointer around is plenty extra efficient than handing the easily merchandise pointed to. desire that enables.
2012-05-05 18:32:23 UTC
http://pw1.netcom.com/~tjensen/ptr/ch1x.htm


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...