1 Building .--------------------.
6 Addresses /____________________ /\
/ / |\
/_____________________/| |
1 Building | +---+ +---+ | |
1 Address | | 4 | | 5 | | |
~ Array --> | +---+ +---+ | |
~ | ------------------- | |
+-----------||----+ | +---+ +---+ | |
/ _ || /| | | 2 | | 3 | | |
/ T / | | +---+ +---+ | |
+-----------------+` | <-- Variable | ------------------- | |
| +------+ | | | +---+ +---+ | |
| / ____ / No.85 | | | | 0 | No.91 | 1 | | |
| +______+ | | | +---+ +---+ | |
| | | .--. | | | ___ | |
| +------+ | | | + | | | | |
| | .| | / | | _,| | ;
| o/ |__| |/ | | | |/
+--/|------/--/---+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~+~~~~~~~~~~~~~
~~~/ \~~~~/ /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==============================================================================
---- ---- ---- ---- ---- ---- ---- ---- ----
==============================================================================
Think of an array as a collection of several variables which are having the same name but meanwhile, each block(or “internal variable”) has its own unique index id.
The variable:
my_num
+-----+
| 7 |
+-----+
And here is the array:
my_array
+-----+-----+-----+-----+
| 9 | 5 | 3 | 7 |
+-----+-----+-----+-----+
It’s a very simple array with 4 elements. Each block(or element) of the array has its own index. Array indexes are numeric and also starting at ZERO!:
my_array <-- name of the array
+-----+-----+-----+-----+
| 9 | 5 | 3 | 7 | <-- values
+-----+-----+-----+-----+
0 1 2 3 <-- indexes
For retreiving a value, in English we’d say “What is stored at the 1st block of my array?” but in C, we’re actually referring to that block as the 0st one![1]
wat? 0st? as in 1st? and 2nd and third?
Yes! They are literally starting at 0st index and in fact, the last block of your array has the index of 3. (0 to 3)
For defining(or declaring or creating) an array in C, we just add square brackets, open and close, right after the “variable” name and by doing so we can turn our simple variable into a “multi-block variable”:
#include <stdio.h>
int main(int argc, char *argv[]){
int my_array[] = { 9, 5, 3, 7 };
return 0;
}
And if we don’t know the initial values, we can use:
#include <stdio.h>
int main(int argc, char *argv[]){
int my_array[4];
return 0;
}
NOTE: Just like the variables, if you define your arrays WITHOUT the initial values, they may(read it: WILL) contain some random junks:
#include <stdio.h>
int main(int argc, char *argv[]){
int my_array[4];
printf("%d\n", my_array[0]);
printf("%d\n", my_array[1]);
printf("%d\n", my_array[2]);
printf("%d\n", my_array[3]);
return 0;
}
If you’re coming from another progamming language such as PHP or any other “on the fly” one, you should know that the C does not support any of those runtime “features” and each name is unique and bound to a specific type of data. (And that’s not true about void pointers however. Later you’ll see how they’re working.)
And if you are not, dude! you can NOT turn a variable into an array or an array into a variable right in the middle of your code! You just can’t!
The following code will result in compile error: [2]
#include <stdio.h>
int main(int argc, char *argv[]){
int my_number = 123;
printf("My number is %d.\n", my_number);
my_number[] = { 3, 5, 8, 13, 21 };
return 0;
}
Which means, you’re doing it the wrong way! (Don’t even try to “bypass” the compile errors, ever!)
Now that you know how to declare them, you may want to know how you can change the value of any specific block as well:
#include <stdio.h>
int main(int argc, char *argv[]){
int my_array[] = {0, 0, 0, 0};
my_array[0] = 2;
my_array[1] = 3;
my_array[2] = 5;
my_array[3] = 8;
printf("%d\n", my_array[0]);
printf("%d\n", my_array[1]);
printf("%d\n", my_array[2]);
printf("%d\n", my_array[3]);
return 0;
}
Yeah, just like the variables! (Variables on steroids.)
Here is what happened in that code:
Initialization:
my_array
+-----+-----+-----+-----+
| 0 | 0 | 0 | 0 |
+-----+-----+-----+-----+
0 1 2 3
Setting the actual values: (Before printing)
my_array
+-----+-----+-----+-----+
| 2 | 3 | 5 | 8 |
+-----+-----+-----+-----+
0 1 2 3
And that’s it!
Now you know pretty much all you need for the next lesson, which is going to be about, “string”s. (Yeah, with double-quotes since the C is technically referring to them as array of chars.)
Practice, practice, and practice!
C you soon!
[1] R.I.P Ye Olde Britain!
[2] Thank you dear C compiler for not letting such a stupid code ever run.