Exercises, examples and other material relating to training module C207. This topic is presented on public courses Learning to Program in C
, Learning to program in C++
, Programming in C
, C and C++ Programming
Variables in C may be declared so that they contain a reference
to another variable rather than a numeric or string value. Using this
facility, you can write generalised code - using a variable called
"current" for example - that you can then point at a particular piece
of data you want to work on. On Monday, "current" can be Lee's
Chinese and on Tuesday it can the The Refa Indian Restaurant ...
|Articles and tips on this subject||updated|
|4128||Allocating memory dynamically in a static language like C|
From last weeks' C course - a complete example showing dynamic memory allocation - reading in any file of data, lines of varying length, and packing them in neat. Source code [here].
There's an issue with reading data of unknown size into a fixed declaration language like C... allocation stack memory ...
|3399||From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)|
Money's useful stuff.
Before money, transactions were undertake on a barter system; if I wanted a loaf of bread I might exchange it for a fish. If I was wanting to book passage on tthe stagecoach from the George in Melksham to London, I would have to take ia bushel of apples into my local travel agent, ...
|3386||Adding the pieces together to make a complete language - C|
Once we've covered the fundamentals of programming on a C course, we move on to cover pointers, arrays, structures, strings, input/output, and dynamic memory allocation. We teach and illustrate each of them, and we have our delegates write practical exercises to make sure that they have a grasp of ...
|3238||Bradshaw, Ben and Bill. And some C and C++ pointers and references too.|
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 ...
|3242||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 ...
|3121||New year, new C Course|
Well - not completely a new course. New delegates, and lots of new examples written for them during the first day to that they can see not only WHAT work but also HOW the design of what works is put together during the day. Click on the individual source code examples, and you'll find programs to ...
|3004||Increment operators for counting - Perl, PHP, C and others|
Look at this Perl statement:
$counter = $counter + 1;
"Take the value in $counter, add 1 to it, and put it back in $counter". It's a common programing requirement - indeed, so common that you can write it in a shorter form in many languages:
$counter += 1;
"Add one to the value ...
|2670||Pointers to Pointers to Pointers - what is the point?|
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)
|2572||The what and why of C pointers|
If you put a "*" in front of a variable name as you declare the variable in C, that variable holds the ADDRESS of a value of the type give, rather than the value itself. Thus:
int bill; /* holds an integer */
int *ben; /* holds an ADDRESS. At that address you'll find an integer ...
|2005||Variables and pointers and references - C and C++|
If I have a variable called "weight" that contains a float, I can use and set its value by using that name.
If - in C or in C++ - I declare a variable to be a pointer then that variable may contain a memory address ... I use a * in my type declaration, and then I use & in my assignment:
|1497||Training Season Starts again!|
I'm glad that our business isn't heavily seasonal - tourist places like Longleat have a heavy summer and a barren winter, and The Christmas Shop must, surely, be heading for a quiet few months.
But we are - slightly - seasonal; we have a quiet three weeks where no-one wants to learn Perl or PHP (or ...
|1478||Some new C programming examples - files, structs, unions etc|
A series of new "C" examples have been added to the web site - a course for a small group allowed me to go somewhat away from the more normal run yesterday and write some new illustrations in front of them.
Call by value v call by pointer comparison: link
Using extern to share a variable between two ...
|1155||Pointers in C|
I know when I'm giving a C Programming course that I'll come to the section on pointers and I'll have to slow right down - those extra * and & characters take a little explaining and getting used to.
Here are the key points.
A variable declared with a * in front of it holds a reference to (i.e. ...
Examples from our training material
|arp.c|| referencing array members through pointers|
|cli2.c|| Handling command line args via pointers|
|dj.c|| Comparing call by name and call by value|
|ex_c207.c|| passing pointers in to a function to allow multiple returns|
|excuse.c|| Pointers - illustrative example 1|
|fifth.c|| Further use of pointers - returning multiple values|
|fourth.c|| function calls - call by value and call by name|
|ip2.c|| Passing pointers to functions|
|makefile|| makefile for module C207|
|p1.c|| First demonstration of a pointer|
|p2.c|| Passing a reference to allow a variable to be change from a function|
|p4.c|| Passing complex data via a single pointer|
|pdo.c|| Hello pointer world|
|pfirst.c|| use of pointer - passing to function|
|plot.c|| Passing an array to a function|
|po.c|| Pointers and addresses - the mechanism|
|psecond.c|| How a pointer can give a temporary name|
|ptr1.c|| Temperature conversions - pointers|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
Declaring and using a pointer
The use of pointers.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.