Main Content
Some new C programming examples - files, structs, unions etc Archive - Originally posted on "The Horse's Mouth" - 2007-12-19 07:57:03 - Graham Ellis
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 different sources: link and link
Handling command line parameters through pointers: link
Structures, and pointers to structures: link and link
Unions, where memory is shared between variables: link
File handling through open and read, fopen and fgets compared: link and link
And some C++ examples:
Fomatting floating point numbers in C++: link
Using a vector (in effect an object that's an array but can be extended as required: link
All examples have comments and sample outputs included and will open in a new window if you click on the links. All simple examples, and they're often the best!
Some other articles
C238 - Templates Using a vector within an object - C++ Reading files, and using factories to create vectors of objects from the data in C++ Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses Templates in C++ - defining a family pattern of methods / functions C++ - unknown array size, unknown object type. Help! Collections in C and C++ - arrays, vectors and heap memory blocks This article C235 - I/O in C++ Formatting and outputting your own classes in C++ Left shift operator on an output stream object - C++ Reading files, and using factories to create vectors of objects from the data in C++ Reading (and writing) files in C++ Printing objects in C++ C++ - unknown array size, unknown object type. Help! C++ - putting the language elements together into a program Comparing Objects in C++ This article C209 - Structures and Unions Adding the pieces together to make a complete language - C Structures v Structure Pointers in C. How, which, why. When is a program complete? C Structs - what, how and why What are Unions (C programming) Using Structs and Unions together effectively in C C - structs and unions, C++ classes and polymorphism This article C207 - Pointers and references Variables, Pointers and References - C and C++ Allocating memory dynamically in a static language like C From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) Adding the pieces together to make a complete language - C How to return 2 values from a function (C++ and C) - more uses of pointers Bradshaw, Ben and Bill. And some C and C++ pointers and references too. New year, new C Course Increment operators for counting - Perl, PHP, C and others Pointers to Pointers to Pointers - what is the point? The what and why of C pointers Variables and pointers and references - C and C++ Training Season Starts again! This article Pointers in C C204 - Functions, Macros and programs in multiple files Function prototype - what they are and why you should use them - C and C++ Preprocessor directives in C and C++ - what they mean Passing information into functions in C - by name, by value Passing arrays into functions in C Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks Returning extra results from a function in C Using functions to keep look and feel apart from calculations - simple C example C Course exercise and sample answer - source in 2 files Sharing variables between files of code in C - extern Function Prototypes in C This article A better alternative to cutting and pasting code Do not duplicate your code