Chapter 5. Structs, Unions, and Bitfields: Roll your own structures
Most things in life are more complex than a simple number.
So far, youâve looked at the basic data types of the C language, but
what if you want to go beyond numbers and pieces of text, and model things in the real world? struct
s allow
you to model real-world complexities by
writing your own structures. In this chapter, youâll learn how to
combine the basic data types into
struct
s, and even handle lifeâs uncertainties with union
s. And
if youâre after a simple yes or no, bitfields may be just what you need.
Sometimes you need to hand around a lot of data
Youâve seen that C can handle a lot of different types of data: small numbers and large numbers, floating-point numbers, characters, and text. But quite often, when you are recording data about something in the real world, youâll find that you need to use more than one piece of data. Take a look at this example. Here you have two functions that both need the same set of data, because they are both dealing with the same real-world thing:
Now thatâs not really so bad, is it? But even though youâre just passing four pieces of data, the codeâs starting ...
Get Head First C now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.