I'll try my best to explain......
Hi Drew,
I'm not really an experienced programmer, but I understand how pointers work and how pointers make C/C++ a powerful language
, and I'll try my best to explain them!
By definition, pointers are variables which store the *memory addresses* of other variables. Why "once you understand pointers you've pretty much mastered the language?" It is because pointers are unique and powerful tool which gives the programmers much control in a convenient way (and that's why I chose C++ instead of Java
). For example, you use pointers to create a dynamic arrays (since you cannot use a variable to declare an array, e.g. int anArray = int[anotherVar]
which size can be declared with variables. In short, pointers make a program more sufficient by allowing the programmers to handle the addresses of a variable instead of the actual objects (which may be huge, wasting computer resources). Like you can pass the addresses to other function, then the code inside the function can call the actual objects using the address (unlike the actual object, which takes only few bytes or less).
For more advance usage of pointers, you can make a binary tree (a heap is a binary tree except heap is a *complete* binary tree). A binary tree (in case you don't know it) is like a sorted structure of data, which is a handy tool to use and data can be found very fast too! You have a root on the top, and then value less than the root go to the left and value greater or equal than go to the right (if i'm wong someone please tell me). Here is why pointers are needed: a node, which makes up binary tree, contains a value (the data) and two pointers, one points to the left and the other points to the right. If you don't understand, see this animated binary tree: http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html (each circle is a node). Basically, pointers enable a node to point to another node because the pointers store addresses, in this case, another node's address. This allows the parent node (or supernode) of the child node be able to locate the child node. Without pointers you can never do this (someone please tell me if there is anyway to do this). Once again, heap is a complete binary tree and has it own order. It is a broad topic, so I cannot explain it here, but check this PDF about heap: http://www.scs.carleton.ca/~nussbaum/courses/cs/95.384/heaps/Heap_Data_Strucure.pdf.
Stack, like heap and binary tree, is another advance method to code. Stack can be coded using array (but vertically). You might have heard this: stack is in LIFO order, that is, Last In First Out. This basically means that a new data is inserted on TOP of the rest, and when you ask the stack to retrieve the data, the TOP data is returned. In short, stack does NOT allow random access; opposite to arrays, any values in an array can be retrieved using anArray[indexNum]. On the other hand, ONLY the value on the TOP can be retrieved. Opposite to Queue, in FIFO (First In First Out) order, queue only allows the value on the BOTTOM to be retrieved; so, if you understand queue, you understand stack.
Stack, queue, binary tree and heap are ALL advance, better way to program. Therefore, unlike array, you cannot declare a stack by using aStack = (stack)stack[5]; or something like that. You have to implement them yourselves as a programmer(or find code already made by others). In conclusion, pointers make these advance way of programming possible. (I think Java have stack and queue too, but it doesn't support pointers; the Java makers implement them already, which makes programming no fun *annoyed*) And this is why I love C/C++.
Sorry for my extremely long reply. Hope this lengthy reply help you in any way. And if you're interested in C++, go to http://www.cpp-home.com, which has a lot of resources on the language
)
(To other programmers: If anything here is wrong, please inform me. Thanks
)
Have a nice one. And Happy Programming