Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Bradshaw, Ben and Bill. And some C and C++ pointers and references too.

Dradshaw Railway Guide, 1960I have a scarse original of the January, 1960 edition of Bradshaw's guide. Bill and Ben are on my course, and they both want to take a look at it. So I take a perfect photocopy of it, and I pass that copy to Bill with the original handed, with great reverance, to Ben.

Bill and Ben both spend some time studying table 138, and when I'm not looking they add some notes for future reader, Bill's in blue ink and Ben's in Green. Both give me a curteous "Thank you" when they're done.

Barabara comes on a course a couple of weeks later, and - talking about rail services as they were in 1960 to Hirwaun, I hand her the old Bradshaw's. "What a pity about these notes in green ink" she says and, indeed, when I look at the old guide I see that it's been defaced in green. And there is no blue ink to be seen.

There's a parallel to this in programming:

• When you call a function, you can "call by value" which means you pass in a copy of the data. That's what we did when we handed the copy to Bill; Bill can read, study and make use of the copy that he's been given but any changes that he makes are to his (second / private) copy, and although he can say "thank you" and tell you his deductions at the end, he's not able to alter the original.

• You can also "call by name" / also known as "call by address". In this case, you're not duplicating the data - you're just telling the function where abouts in the computer memory it's to find the original so that it can access it directly. Much more efficient, but more flexible, but also much more trusting (by the calling code) of the function. Any alterations made by the function are reflected in the original.


In C and C++, You can use both "call by value" and "call by address". Here's a call to a fucntion which passes in a value:
  lots = triple(stuff);
and here's one that passes in an address:
  oodles = quadrle(&thing);
With the & meaning "memory addresss of"

Within the function, I have to declare my data type to be a regular variable in the first case, or a pointer in the second case:
  int triple(int incoming) {
and
  int quadrle(int * incoming) {

And then I have to use "*" to mean "Contents of" as I reference the variable in the pointer / call by address version. So in the first function I write
  outgoing = incoming * 3;
  incoming = incoming - 4;
but in the second function I write
  outgoing = *incoming * 4;
  *incoming = *incoming - 5;
with the first example NOT altering the value of "stuff" in the calling code, but the second example ALTERING the value of "thing" in the calling code.

C++ also lets you declare references - which are lightweight aliases to variables and gibe you much of the power or pointer without the need to use the extra * as you access the variable's content. And it's critically important that you understand the whole business of pointers and reference if you're using C and C++. I find that parallels, comparisons such as the one I started with help delegates get to first base on the subject on our C and C++ courses, and we introduce the subject early on so that everyone gets plenty of chance to get plenty of exposure to and practise with them ... getting to 2nd and 3rd base during the rest of their course. There's a C++example showing references [here].

The complete source code used for the example lines above is [here] ... and I've also added some sample output so you can see how it works.

Want to learn nore? Come on our C or C++ course - we've got different course to suit you no matter what your starting point! [link to choose]
(written 2011-04-09, updated 2011-04-17)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C207 - C and C based languages - Pointers and references
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [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)
  [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)

C234 - C and C based languages - Further C++ Object Oriented features
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2576] What does const mean? C and C++ - (2010-01-15)
  [2004] Variable Scope in C++ - (2009-01-22)
  [1819] Calling base class constructors - (2008-10-03)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)
  [801] Simple polymorphism example - C++ - (2006-07-14)


Back to
Using functions to keep look and feel apart from calculations - simple C example
Previous and next
or
Horse's mouth home
Forward to
TrainWest, 2011 in pictures - Christie Miller, Bowerhill, Melksham
Some other Articles
Spring in the countryside near Melksham
Melksham Town Council - vacancy in the Spa Ward
TrainWest, 2011 in pictures - Christie Miller, Bowerhill, Melksham
Bradshaw, Ben and Bill. And some C and C++ pointers and references too.
Using functions to keep look and feel apart from calculations - simple C example
C - a first program that does something useful for you
How we make our programming courses both time and cost effective
Your program - you just provide the filling in the sandwich
C / C++ Course Lunch - sitting out at the West End
4280 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, 83, 84, 85, 86 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3238_Bra ... -too-.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat