The use of pointers...
I understand how to use pointers and all but why would you use them? Why would you use a pointer to a variable when you can just use the pointer? The only reason I've found is to use variables in different functions (if they're not global).
Thanks in advanced.
what do you know about pointers?
C++ is not language xyz. Other languages don't need pointers because, by design, that language wouldn't need to indirectly refer to an object through a memory address alone. C++ on the other hand is different. A pointer is the only way to know where something is on ocassion.
can you tell me more about pointers, cause im sort of lost with them too
>> I understand how to use pointers and all but why would you use them?
This exact question is asked a lot. Here are two older threads that try to answer it and include some other relevant discussion:
the only time i can see a use for a pointer, i could probably do the same with a reference
Thanks a bunch. I still don't 100% understand but I read in one of those links that they are only really important for more "dynamic programs". So long as I know how to use them (not when), that should be sufficient for now.
Good luck iterating through an array with a reference. References cannot change the object they refer to. Pointers can.
Originally Posted by Hobgoblin
> References cannot change the object they refer to.
Oh no? Then explain the output of this program...
void foo(int& bar)
bar = 1;
int bar = 0;
std::cout <<bar <<std::endl;
Not change the contents of the object, but start referencing another object
Originally Posted by UMR_Student
Yeah, I totally read that wrong. Sorry.
Languages such as Java, for example, have no pointer data type that the programmer can explicitly declare. However, behind the scenes, most everything is actually a pointer. Learn some assembly language and you will see that.
In C++, virtual functions can be used to replace function pointers. But, there is really still a function pointer in there. I believe the goal is to reduce complexity and potential errors by the programmer. But there is something to be said for a programmer who learns how everything works behind the scenes.
And there are some instances in C++ where a reference will not do. For example, references (which are really just a const pointer, logically), cannot change what they point to. Even as a amateur programmer, I have many times needed to have a pointer change what it's pointing at.
I came to appreciate pointers more as I learned more. I think once my programming branched out from a sequence of commands and if-statements, I saw the need for pointers.
Depending on the book/tutorial you're using to learn C++, you could be forgiven for thinking that pointers are a novice-beginner subject (Far too many books & tutorials present them this way) - in fact, the language standard goes to great lengths to protect beginners against the need for pointers until much later on (Not so for 'C' where pointers are more of a bread and butter issue) The availability of references and STL greatly reduces their practical use in simple programs, leaving pointers for more advanced topics, such as the following;
* More in-depth OO techniques such as polymorphism and (GoF) design patterns
* Dynamic memory allocation (Something which is best avoided in C++ when possible - although plenty of old-style tutorials insist on teaching this before delving into the STL)
* Manipulating 'raw' arrays (Ideally, a beginner will already have a firm idea about STL Containers & Iterators before doing this - again, old-style tutorials usually do this the other way around)
* Dynamic data structures - eg, linked lists, binary trees, etc
If these concepts fly over your head, then you're probably not in a position yet where pointers are of any significant use to you. IOW, don't worry about them just yet - focus your effort on understanding the basic language features and the STL for now
I don't see where c++ wants to protect the programmer from pointers. If it would do so there would be
So you need to know about pointers for the simplest possible c++ program.
int main(vector<string> argv);
// instead of
int main(int argc, char** argv);
And even if the first version of main would exists one wouldn't be able to list all command line arguments without dereferencing the iterator.
Better get pointers right.