Showing posts with label structs. Show all posts
Showing posts with label structs. Show all posts

Friday, April 5, 2013

structs


A struct in C is a complex data type, like an array, except each element can be of a different type.

Each element, known as a member, has its own name (not a subscript) and its own type. A member can itself be a structured type. A structcorresponds to the "record" structure in other languages.

structs Example





The above snippet of code is a structure template ( as the structure declaration is not followed by variable names); the name "studentRecord" is a structure tag; the variables within are called members or components of the structure.

Whilst an array is an aggregate of values of the same type, a struct is an aggregate of inhomogeneous values ( that is, of distinct type).

We can then declare variables student1, student2 of this type by e.g.:

Alternatively, variables can be declared with the struct definition e.g. :

but we shall see a better way soon.

Once variables have been declared, we reference individual members using the 'dot' (.) operator: