Composite C Data Types

C supports simple arrays. The initial values in an array are undefined, and the number of elements is a constant set at compile time.

int arrayOne[10];

//Creates an array of ten ints, arrayOne[0] to arrayOne[9]

//The initial values are undefined, and the number of elements is a constant

Use the struct keyword to create a composite data type made of fundamental data types. structs are rather like the ancestors of Objective-C’s objects. They’re simpler, they don’t support methods, and they don’t use Objective-C’s automatic memory management.

struct playerOne{

char name[20];

int xpos;

int ypos;

}

A union is like a struct and is defined in a similar way, but it can hold only a single value. In the following example, the union can hold a character array, an int called xpos, or an int called ypos. If you set one element in a union, the other elements become undefined. (This exotic behavior saves memory, but it’s largely unnecessary now.)

union playerOne{

char name[20];

int xpos;

int ypos;

}

Get iOS App Development Portable Genius 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.