A normal variable we want to an array and it’s an open and closed square brackets whenever I use these open and close square brackets after the.
Name it’s gonna tell C like okay we want to.
Store multiple pieces of information now there’s a bunch of different ways we can create these arrays the easiest one though is to just say equals and I can make an open and closed curly.
Bracket and inside of this open and close curly bracket I can just start typing out some numbers that I want to put in this array so we could type out like 4 8 15 16 23 42 now I can basically.
Type out as many numbers as I wanted and I could store them inside of this array so over here I you know I printed out six numbers I could print out 600 if I wanted you know the only thing that’s limiting me is how many I can actually put in here so you’ll notice that I put in the piece.
So like 4 for example and then I put a comma and a comma is gonna separate all the elements in the array so we would say 4 is the first element in the array 8 is the second element in the array these are all considered to.
Be elements inside of this array.
And what’s cool about this array is it’s now storing all of this information so unlike a variable where we can only store one value now in this array I’m storing 6 different numbers so if I wanted I can print this out and I’m actually gonna print this out just to show you guys so remember we’re gonna print out a number and I’m gonna.
Show you guys how we can access specific elements inside of this array so all of these numbers are stored in this lucky numbers array in this array structure but the question becomes how do we access them right how can I gain access to one of these well.
I could say present’ d so I’m gonna print this out and now I can just say lucky numbers so I’m gonna refer to the name of the array and when I want to access a specific element I can make an open a close square bracket and inside of this open and close square bracket.
I want to put the index of the element that I want to access so all of these elements in here have a specific index and I can access them by putting the.
Index inside of this square bracket so if I wanted to access this for for.
Example I can put a 0 inside of here and now when I run my program you guys will see we’ll be able to print out that for so over here I’m printing out four if I wanted to.
Access this 15 I can put a two in here so now when I run my.
Program I’ll be accessing the 15 as you can see so one thing you might have noticed by now is that we start the indexes and.
Arrays at 0 so when I wanted to access this 4 instead of putting a 1 in here I put a 0 in here right you might think that 4 would be at index position 1 because it’s the first element in the array but in C we start array.
Indexes at 0 so actually the first element this 4 is gonna be at index position 0 so if I was going to write out indexes I.
Would say like 4 is at index position 0 this 8 is at index position 1 this 15 is at index position 2 16 is a 10x position 3 etc so that’s gonna allow me to access all these elements inside of the array individually so I could print them out just by referring to the index inside of these square brackets another thing I.
Can do is I can modify some of the elements inside of here so let’s say I want to modify lucky numbers in x position 1 so let’s say I want to modify this 8 so I don’t like this 8 anymore so I can basically just assign this a different value like I normally would.
So I could say lucky numbers and I can make an opening close square bracket we can say 1 and I can just set it equal to something else so I could set this equal to like 200 and now when we print out lucky numbers 1 instead.
Of printing out an 8 it’s gonna be printing out a 200 so let’s go ahead and do that and run my program and you’ll see.
We’re printing out 200 instead of 8 so you can modify any of the individual elements inside the array just by referring to the index and.
Really when you think about this conceptually an array is basically just holding a bunch of variables but those variables don’t have names so when I access lucky numbers 1 it’s the same as me accessing an integer variable it’s just that you know the array is holding potentially hundreds or thousands or millions of these different variables so an array is.
A very useful structure especially if you’re storing data that is very similar so like I said I could do this with an N I could also do this with a double I could do this with a float I can do this with whatever I wanted but there’s gonna.
Be situations where you might.
Not necessarily know all the elements that you want to put inside your array so let’s say I’m creating an array and I don’t know what numbers I want to put in it yet so.
Maybe I just like want to create it I want to tell C that we need it but I don’t want to necessarily put anything in it right away well up here we’re basically saying lucky numbers that open and close square brackets and then we’re giving it a bunch of information but if.
We don’t want to give it all that information instead I can just put a call a semicolon.
Here but inside of these square brackets I need to tell see how many elements that this array can hold so I need to tell C like hey this array can only hold like ten elements.
Or this array can hold 20 elements or whatever so let’s say we wanted an array that could.
Hold like ten elements I can put a 10 right here and now the capacity of this integer array is 10 so it has.
The potential to hold 10 items and what I could do is I could come down here and I can start assigning values to those items so I could say like lucky numbers 1 is equal to 80 right so I’m giving lucky numbers 1 of value and now over.
That value out onto the screen.
And you’ll see we’re printing of 80 but if I try to print out for example lucky numbers 0 so the lucky numbers at in X position 0 this is gonna give me a negative 2 which basically means that it’s not found so it basically means that there’s no element inside of lucky numbers at index position 0 but if I wanted.
I could do that so I could say like lucky numbers 0 is equal to 90 and now when I run my program it’s gonna be able.