Pointers to Pointers to Pointers - what is the point?
Archive - Originally posted on "The Horse's Mouth" - 2010-03-10 18:35:21 - Graham EllisFrom today's course - a new example showing pointers to pointers to pointers. But why?
C pointers are useful in many ways:
• they let you pass a single value into a function that holds a whole collection of data
• they let you call by 'name' so that functions can alter values (see here)
• they let you access additional dynamic memory (example of that - here)
• they let functions return an item that's of unknown size.
But why pointers to pointers? Because they let you hold and process collections withing collections easily. As well as the first example, we have another example that does this - pascal's triangle all in dynamic memory - here.
And in real life, it can represent a whole data heirarcy too.
• I have a number of food product objects
• Each is in a dish object
• A number of dish objects meal up a meal object
• and we build up a collection of meal objects into a daily diet.
And we could go on.
• A familiy's daily diet objects makes a shopping object
• and a store's customers each day make up a turnover
I think I may have six levels of pointers there.
This all gets very hard to write and to look after; structured coding can help, but far better to use objects, which is where C++ comes in. I'm teaching a C++ Course tomorrow ... so I expect you may find some more examples along these line on the next blog entries.