O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Mikey Ward, Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11 Structs

Sometimes you need a variable to hold several related chunks of data. In C, you can do this with a structure, commonly called a struct. Each chunk of data is known as a member of the struct.

For example, consider a program that computes a person’s Body Mass Index, or BMI. BMI is a person’s weight in kilograms divided by the square of the person’s height in meters. (BMI is a very imprecise tool for measuring a person’s fitness, but it makes a fine programming example.)

Create a new project: a C Command Line Tool named BMICalc. Edit main.c to declare a struct named Person that has two members: a float named heightInMeters and an int named weightInKilos. Then create two Person structs:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required