|
How to return 2 values from a function (C++ and C) - more uses of pointers
A further example (following previous article [here]) of where pointers are particularly useful is where you want to return nore than one resulting value from a function ... in C, that's something you couldn't really do without them.
There are two ways:
a) A function returns a single value ... but you can make that return value a pointer, so that you can reurn a whole array or structure of values and
b) You can pass in a pointer to your function, so that the value(s) at the address passed in and at subsequent memory addresses can be modified withn the function, thus forming a second return.
Let's see an example of where you might wish to do this, written as a practical demonstration on last week's run of our public C programming course.
I have £150.00 which I want to spend on the hire of a forklift truck for as long as possible. Hire costs £22.00 per month, £10.00 per week and £3.50 per day ... how long can I have the forklift truck for?
code:
float bank = 150.00;
int month, week, day;
month = spend(&bank,22.00);
week = spend(&bank,10.00);
day = spend(&bank,3.50);
The spend function has two inputs - the amount that's available (your balance) and the cost of the forklift truck for one unit of time. It returns the number of complete time units. And it alters the balance in the "bank" variable - it's able to do so because it's the address that's been passed in.
Compelete source and sample output - [here]. The image that accompanies this article is public domain and we claim no copyright over it. (written 2011-04-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) [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) [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10) [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)
55fd
Some other Articles
Melksham - the way forward. 26th April, Well House ManorCollections in C and C++ - arrays, vectors and heap memory blocksC and C++ - preprocess, compile, load, run - what each step is forBreaking the running sequence - an introduction to conditional statements and loopsHow to return 2 values from a function (C++ and C) - more uses of pointersSpring in the countryside near MelkshamMelksham Town Council - vacancy in the Spa WardTrainWest, 2011 in pictures - Christie Miller, Bowerhill, MelkshamUsing functions to keep look and feel apart from calculations - simple C example
|
4083 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|