You've already started watching Array
An array in C is a mechanism for storing multiple values of the same data type. The values are indexed for easy access. The index always starts at zero.

0:00
In the previous video, we learned about the 3 fundamental variable types in C—

0:05
int, float, and char.

0:08
These types are fine for dealing with a single letter—char,

0:12
or number of days in the week—int,

0:15
or some physics constant—float.

0:18
But think about it for a moment. That is one heck of a limitation.

0:23
How would we represent a list of the days in a week?

0:26
Or months in a year? For that we need arrays. Let's take a look.

0:31
Okay, arrays, let's do some C hacking and get into it.

0:35
First, we go to our Development folder and create a subfolder called hello arrays.

0:45
Make a copy of MyFirstCProgram,

0:48
place it in the folder, and open the project file in Xcode.

0:56
Select main.c and get rid of the print statement.

1:01
We're going to declare an array of floating point variables.

1:05
Let's do that now.

1:08
We'll call it numbers_geeks_love.

1:12
We'll make it large enough to hold 3 floating point numbers.

1:16
We indicate that by [3].

1:20
We're now going to load this array with 3 floating point numbers.

1:25
We do that by accessing our array at different indices or indexes.

1:30
In C the first item in an array is the zeroth item, not the first item,

1:34
So, let's fill that out right now.

1:37
The first item in our numbers_geeks_love array will be pi.

1:44
The second number will be a golden ratio.

1:48
The second number will be stored at the second index,

1:51
which you indicate by 1.

1:55
We'll add a third number at the third index,

1:59
which we indicate by 2.

2:03
This will be the square root of 2.

2:06
The reason arrays are zerobased is as follows—

2:10
an array in C, specifically the variable name,

2:13
is a pointer to a physical computer memory address.

2:17
Wait! Don't freak out. You'll learn all about pointers later in this course.

2:21
When we access an array, or index into an array in geek speak,

2:26
we are selecting an offset from the computer memory address

2:30
pointed to by the name of the array.

2:33
Here, the name of the array is numbers_geeks_love.

2:36
The first array item in that array

2:39
is at a memory address offset of 0.

2:43
So, going back to our array we see the first item is at 0

2:47
the second is at one, etc.

2:50
Now we have these variables, now we have this array,

2:53
let's print the values in the array.

2:56
So, we write our friend the print statement,

2:59
and the first number we'll print is pi.

3:02
We use the formatting symbol for a floating point number.

3:05
And now, we're going to access the first number in the array.

3:10
Again, we use 0.

3:13
So, here we see when we loaded the first item we loaded it into 0.

3:18
Now, we're retrieving the first item so again we use 0.

3:22
Let's print that.

3:26
Pi is 3.1415.

3:29
Let's close the console and continue printing our numbers.

3:34
Let's print the second number in our array—

3:36
1.618, the golden ratio.

3:39
[typing]

3:43
Again, we index into our array numbers_geeks_love,

3:47
but this time we use 1 as opposed to 0.

3:51
So, let's look closely at that—the first number was at 0,

3:55
the second number is at 1, so we're printing the second number, which is at 1.

4:01
Let's print that—golden ratio.

4:05
Finally, let's print the square root of 2.

4:09
The square root of 2 is the third number in the array.

4:12
The third number in the array is at index 2.

4:15
[typing]

4:20
We type 2 to access the third number, and now we'll print all 3 numbers in our array—

4:27
pi, golden ratio, square root.

4:31
So now we've seen a floating point number stored in a floating point array.

4:36
Let's do something slightly different—we'll look at integers.

4:39
We'll make an int array called primes, for prime numbers.

4:44
Here we type an open square bracket

4:47
followed by curly braces, and in the curly braces,

4:51
we'll insert the numbers we want in our integer array.

4:55
These will be the first prime numbers 2, 3, 5, 7.

5:01
So, let's stop for a moment—

5:03
notice we didn't have to insert an number for the size of the array

5:07
where we're initializing the array for the first time.

5:12
Now, let's print the prime numbers—the first 4 prime numbers.

5:21
Here we use the integer formatting symbol indicated by %d.

5:28
We use 4 formatting symbols for the 4 numbers that are in the array.

5:39
Again, the first number in our array starts at 0,

5:41
and I'll just make a copy to do this a bit quicker.

5:45
The first is 0, the second is 1, the third is 2, the fourth is 4.

5:53
End with a semicolon—now let's print.

5:57
So, we have our floating point array with some numbers geeks love

6:01
and we have our array of prime numbers, which are integers.

6:05
And there is a bug—so, this is a teachable moment.

6:09
Notice we placed array index 4—let's take a look at this error—

6:14
it says array index 4, but we know our indices are 0, 1, 2, 3—

6:23
there is nothing at index 4.

6:26
So, if we go back to the print statement, replace it with a 3,

6:30
now run our program—2, 3, 5, 7—the first 4 prime numbers.

6:39
Using this approach where we declare our array

6:42
and initialize it at the same time

6:44
is often very convenient and makes our code more compact and more readable.

6:49
Let's go back and change numbers_geeks_love to do it the same way we did primes.

6:57
We remove the number 3 and replace it with nothing,

7:00
and now we just copy and paste these 3 numbers,

7:06
pi, golden ratio, square root of 2,

7:12
and we can eliminate these 3 lines of code.

7:16
Now, let's print.

7:19
Now you have a feel for the use of arrays in C.
You need to sign up for Treehouse in order to download course files.
Sign up