Exercises, examples and other material relating to training module C205. This topic is presented on public courses Learning to Program in C
, Learning to program in C++
, Programming in C
, C and C++ Programming
If you want to hold a who table of different pieces
of information under a single variable name, you can use
an array. This module deals with the declaration of arrays
in C, and how you write programs that will step through and
handle each element of the array in turn without having to
|Articles and tips on this subject||updated|
|3245||Collections in C and C++ - arrays, vectors and heap memory blocks|
If you want to hold multiple values within a table in a program, you'll want to use some form of collection or table - and in many languages including C++ and C, the most straightforward type of collection is an array. An array occupies sequential locations in the computer's memory when your program ...
|3144||Setting up arrays in C - fixed size at compile time, or dynamic|
You can store a whole series of data values of the same type in an array in C.
The easiest way to declare an array is something like:
and that will give you an array of 20 elements - which you can refer to as elements number 0 to 19, so:
history = 1234;
|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 ...
|3118||Arrays of arrays - or 2D arrays. How to program tables.|
It's shorthand when you're learning a new programming language to ask "how does it handle multiple dimension arrays" ... but in practise very few languages actually support true multidimensional arrays these days, and in those which do you might be well advised to use an alternative that's more flexible.
|2840||Just pass a pointer - do not duplicate the data|
When you want to pass a big bundle of data - such as an array - into a function, it's far more efficient to pass in just a single value in the form of a pointer to the array. That's the address in memory at which the array starts and that single value can provide the key to all the data without havig ...
|2002||New C Examples - pointers, realloc, structs and more|
Every time I program in C, I marvel at how clever the language is ... yet at the same time I curse some of the devices that are used to perform certain actions, which make the code that much more of a 'puzzle' to right.
I've finished a 2 day C Programming course today ... and written a whole raft ...
|1614||When an array is not an array|
An array, I was taught, is a sequential series of memory locations in which values of some type are stored. In an array, each of the memory locations is the same size (number of bytes). And so, as I was taught, arrays can be used very efficiently using pointer arithmetic, BUT they need to:
a) Be defined ...
Examples from our training material
|abits.c|| extern - a global variable|
|abits.h|| header file for global variable demo|
|afp.c|| Pointers and Arrays - Interaction|
|amain.c|| Use of constants and globals|
|arr1.c|| Arrays - triangle numbers|
|arrbad.c|| Arrays - triangle numbers WITH ERROR|
|eleven.c|| Array - declaration, filling with data, reusing |
|lp.c|| declaring an array and defining its length|
|makefile|| makefile for module C205|
|morearr.c|| Arrays - days and months|
|pa2.c|| Passing an array to a function - (2)|
|pa3.c|| Allocating heap memory and passing to a function|
|paif.c|| Passing an array to a function - (1)|
|par.c|| pointers v arrays |
|rainfall.txt|| Sample rainfall data (1) - always 31 days of data|
|series.c|| read array, print in reverse, min and max|
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 initialising an array
Techniques for iterating through an array
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.