Tuesday, July 14, 2009

Im having trying to understand the concept of pointers in c++?

I understand that they point to the location of the integer but why would u use a pointer. It makes no sense to me. Many program use them but why, Thnx

Im having trying to understand the concept of pointers in c++?
Pointers are used alot in data structures (as well as many other things).





For example, you can have what is known as a linked list. This is a list that contains items that link to the next item in the list via pointer. This allows you alot of flexibilty and speed in "moving around the data" logically because you simply have to change the pointer, not shift all the data around in memory.





This is an example of a few items in a linked list:





Name: NodeA


Data: A


Next: Address of NodeB





Name: NodeB


Data: B


Next: Address of NodeC








Name: NodeC


Data: C


Next: Address of NodeD





etc.





Now if you wanted to delete node b, you would just have to change the next on node a to node c, rather than having to delete the data for node b and then move all items after it down a section to take its place.
Reply:Pointers are used for several reasons.





Normally you would use a pointer to point to an object. That object may have different characteristics based on something in the record. In one instance it may point to car object and later a bus object. It can be easier to handle using pointers.





Pointers can also be used point to an parameter to a function call. This allows the function to operate on the real object rather than a copy. Passing copies back and forth can be wasteful in both time and space.





Text me if I haven't helped.


No comments:

Post a Comment