From 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.
(written 2010-03-10)
Associated topics are indexed under
C207 - C and C based languages - Pointers and references [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3121] New year, new C Course - (2011-01-05)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[2572] The what and why of C pointers - (2010-01-13)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[1497] Training Season Starts again! - (2008-01-07)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1155] Pointers in C - (2007-04-19)
Some other Articles
Make and makefiles - a commented example to help you learnMultiple Inheritance in C++ - a complete exampleDear Planners, please provide viable alternativesMelksham - Carnival, Party in the Park, and Spot the OddityPointers to Pointers to Pointers - what is the point?Efficient use of dynamic memory - C and reallocIs it worth it?Web page to telephone calls / links using an iPhoneRandom thoughts on Melksham Town Planning and developmentEarly Spring walk in Melksham