1
Arrays are a really fundamental data structure for any programming language that you will use.
We use arrays to hold values of the same data type at contiguous memory locations.
That is to say, it's a way that we can group a bunch of integers together in memory or a bunch of characters or floats in memory really close together and work with them without having to give each one its own unique name, which can get cumbersome after a little while.
Now, one way to analogize arrays is to think about your local post
office for a second.
Usually, in most post offices, there's a large bank a post office boxes on the wall.
2
An array is a giant block of contiguous memory, the same way that a mail bank in your post office is a large space on the wall of the post office. Arrays have been partitioned into small, identically sized blocks of space, each of which is called an element, in the same way that the wall of the post office has been partitioned into small, identically sized blocks of space, which we call a PO box.
Each element of the array can store a certain amount of data, just as each post office box is able to hold a certain amount of mail.
3
What can be stored in each element of the array is variables of the same data type, such as int or char, just like in your post office box,
you can only fit things of a similar type,
such as letters or small packages.
4
Lastly, we can access each element of the array directly by index number, just as we can access our post office box by knowing its mailbox number.
5
In C, the elements of an array are indexed starting from 0, not from 1.
So if an array consists of n elements, the first element of that array
is located at index 0, and the last element of the array is located at index (n - 1).
Unfortunately, or fortunately, depending on your perspective, C is very lenient(宽大) here. It will not prevent you from going out of bounds of your array.
You could access the minus 3 element of your array or the 59th element of your array, if your array only has 50 elements.
It won't stop your program from compiling, but at run time, you might encounter a dreaded segmentation fault if you start to access memory that is outside the bounds of what you asked your program to give you.
So do be careful.
6
What does an array declaration look like?
How do we code an array into existence like we code any other variable?
Array declarations:
type name[size];
There are three parts to an array declaration-- a type, a name, and a size.
This is very similar to a variable declaration, which is just a type and a name, the size element being the special case for an array, because we are getting a bunch of them at the same time.
7
Arrays are not restricted to a single dimension, which is pretty cool. You can actually have as many side specifiers as you wish.
for example:
type name[size][size];
And then, you can choose to interpret this in your mind as a (size) by (size) grid of cells.
Now, in fact, in memory, it really does just remain a (size*size) element, single dimensional array.
8
Now, something really important about arrays.
We can treat each individual element of the array as a variable. But we can't treat entire arrays themselves as variables.
We cannot, for example, assign one array to another array using the assignment operator. It's not legal C.
If we want to, for example-- what we would be doing in that example would be to copy one array into another. If we want to do that, we actually need to use a loop to copy over each individual element one at a time.
The correct way:
In other programming languages, more modern ones, you can, in fact, do just that simple equals syntax.
But C, unfortunately, we're not allowed to do that.
9
Now, there's one other thing I want to mention about arrays that can be a little bit tricky the first time you work with them.
We discussed in a video about variable scope, that most variables in C, when you call them in functions, are passed by value.
Do you remember what it means to pass something by value?
It means we're making a copy of the variable that's being passed in.
The callee function, the function that's receiving the variable, doesn't get the variable itself. It gets its own local copy of it to work with.
Arrays, of course, do not follow this rule.
Rather, what we call this is passing by reference. The callee actually does receive the array. It does not receive its own local copy of it.
And if you think about it, this makes sense.
If arrays are really large, it takes so much time and effort to make a copy of an array of 100 or 1,000 or 10,000 elements, that it's not worth it for a function to receive a copy of it, do some work with it, and then just be done with the copy; it doesn't need to have it hanging around anymore.
Because arrays are some bulky and cumbersome, we just pass them by reference.
We just trust that function to, don't break anything. So it does actually get the array. It doesn't get its own local copy of it.