01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

48 CHAPTER 2 Data types, variables, and constants<br />

2.4.3 Arrays<br />

Like variables of primitive data types, structure variables can be initialized with an initial<br />

value. You can achieve this through two forms of syntax. The first is to provide a<br />

value for each field in a set of curly braces:<br />

struct box a = { 10, 20, 30 };<br />

This line specifies a box 10 wide 20 high 30 deep. The values in the curly braces are<br />

specified in the same order as the fields were defined in the structure declaration. If<br />

you don’t specify enough values, any unspecified fields are left uninitialized. An alternative<br />

syntax explicitly specifies the name of each field being initialized, as follows:<br />

struct box a = { .width=10, .height=20, .depth=30 };<br />

This alternative syntax can be clearer in intent and will also cope if the order of the<br />

fields in the structure declaration changes. It’s even possible to initialize only select<br />

fields in the structure when using this syntax:<br />

struct box a = { .depth=30 };<br />

This last example sets the depth field to 30 and leaves the width and height fields<br />

uninitialized. In this sense, uninitialized means the field will have the value 0 or its<br />

equivalent, such as nil, although this depends on the storage class of the variable.<br />

Structures are a handy way to group related sets of variables into easier-to-manage<br />

chunks, but they don’t solve all of the problems you may encounter with storing<br />

details about multiple highly similar objects. For example, you may need to calculate<br />

the total width of all the boxes in your application. To do this, you could come up with<br />

an expression similar to the following:<br />

struct box a, b, c, d, e, f;<br />

int totalWidth = a.width + b.width + c.width + d.width + e.width;<br />

Although this is manageable with only five boxes, imagine how torturous the expression<br />

would be to write if the application instead needed to store details of 150 different boxes.<br />

There would be a lot of repetitive typing involved, and it would be easy to miss a box or<br />

include a box multiple times. Each time you change the number of boxes your application<br />

needs to store, you’d also need to update this calculation. That sounds like a lot of<br />

pain! What you want the expression to say is “add the width of every box” and have the<br />

statement work without modification no matter the number of boxes your application<br />

currently keeps track of. Not surprisingly, <strong>Objective</strong>-C has a data type to help you out.<br />

Another common scenario is the need to store multiple values of the same data type.<br />

C provides a handy data structure called an array to make this task easier. An array is a<br />

data structure that stores a list of items. Each item (or element) is stored consecutively,<br />

one after the other, in memory and can only be accessed by its relative position.<br />

This position is often called the item’s index. To declare an array capable of storing<br />

details of 150 boxes, you could write a variable declaration similar to this:<br />

struct box boxes[150];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!