Structures in C Programming Language

This post contains some very advanced but important features of the C programming language.

Having read this post you should be able to:
  1. program using a structure rather than several arrays.
  2. how pointer can be used in combination with structures to form linked list.

Structures

The array is an example of a data structure. It takes simple data types like intchar or double and organises them into a linear array of elements. The array serves most but not all of the needs of the typical C program. The restriction is that an array is composed of elements all of the same type. At first this seems perfectly reasonable. After all why would you want an array to be composed of twenty chars and two ints? Well this sort of mixture of data types working together is one of the most familiar of data structures. Consider for a moment a record card which records nameage and salary. The name would have to be stored as a string, i.e. an array of chars terminated with an ASCII null character, and the age and salary could be ints.
At the moment the only way we can work with this collection of data is as separate variables. This isn't as convenient as a single data structure using a single name and so the C language provides struct. At first it is easier to think of this as a record - although it's a little more versatile than this suggests.


Learn More :