O'Reilly logo

iOS App Development Portable Genius by Richard Wentk

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

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;

}

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