Main Content

Bradshaw, Ben and Bill. And some C and C++ pointers and references too.

Archive - Originally posted on "The Horse's Mouth" - 2011-04-09 20:52:23 - Graham Ellis

I 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]