Initializing Array Elements
Just as you can assign initial values to variables when they are declared, you can assign initial values to the elements of an array. This is done by simply listing the initial values of the array, starting from the first element. Values in the list are separated by commas, and the entire list is enclosed in a pair of braces.
The statement
int integers[5] = { 0, 1, 2, 3, 4 } ;
sets the value of integers[0]
to 0
, integers[1]
to 1
, integers[2]
to 2
, and so on.
Arrays of characters are initialized in a similar manner; thus, the statement
char letters[5] = { 'a', 'b', 'c', 'd', 'e' } ;
defines the character array letters
and initializes the five elements to ...
Get Programming in Objective-C, Sixth Edition 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.