Course Content

Course Content

Declaration & Initialization of Arrays

Hello everyone, my name is Aaditya… and we are continuing with this C programming course.

(7 seconds pause ; music)

In the last video, we had discussed what is the scope of a variable, we saw that when we are making one variable, so what is that place that they are making it in.0

That also becomes very important for us.

This scope can be basically of 3 types.

In which the 1st type of scope for any variable

would be a ‘local variable’.

After that we see another type of variable or other type of scope which we call a ‘global variable’.

After that we saw our formal parameters.

After knowing all these things our function chapter gets over or gets closed.

We have known all the things about the function and with this, we are starting with another topic which is called Derived Data Types.

In the last few videos, we had talked about the data types and we had seen that there are different types of data types like our basic data types.

In the same way, our ‘derived data types’ are there too, how do they work basically?

They are made out of the basic data types which have few different purposes which we would see ahead in this video.

In the derived data types comes arrays, union, structure or any such type of data structures.

All those basically come in derived data types.

(01/42)

We will try to understand and learn all of these one by one and we will see how they are different from each other.

The first derived data type that we are going to learn is called ‘arrays’.

Now we will go ahead and see what is the meaning of array and what is the need of it?

So, we will take one example.

Suppose we have a student and that student has given an exam in 5 subjects.

After giving exams in 5 subjects, he also got marks in 5 subjects.

And we have to basically store in 5 variables.

So, what we will do is, we will make 5 different variables with different names and we will store these marks there.

But if I say that I want to store 100 subject marks for the same student.

Even though it is logically incorrect, if I say that one student has given an exam of 100 subjects and his 100 different subjects marks have come to us, we have to store them.

Will I be able to make 100 different variables for that particular mark entry? I will not be able to make it, even if I make it, it will be very difficult for me to keep in mind the names of every variable.

In the same way, if I want to make 1000 or do the counting in those numbers or I have got some data stored.

In this case making variables would be impossible for me.

In this situation, array comes to my rescue.

Now what is an array?

Basically, one type of variable that is there, it collects all the variables and makes a sort of a chain.

And this chain is known by one name.

Like I said, the way we were doing the marks entry, I would say that these marks should be stored in integer type of variables.

So, what I did was I took many such variables in which I stored the marks.

Since it would be difficult for me to remember the names of each variable.

Since all were integer types, what I did was I clubbed everyone and made a chain.

I put them one after the other.

And I would know this chain basically by one name.

Which we will call an array.

clear guys !

So, what we did was, we made an integer type of array.

Array means it is a chain basically of different variables whose data type would be the same.

So, we will also learn about these further.

Now, we will see here quickly what its definition says.

So, the definition says that,

“an array is used to store a collection of data and it is often used as a collection of variables of the same type”.

So, this is what we have understood in layman terms.

When we have to put the same type of data in different blocks.

At that time, we make arrays.

And why are the arrays easy, because we have to remember just one name of that array.

With that name itself, all the variables which we attach to each other by that chain, we get all the information about them and we can extract the values from them.

So, basically this is the example of our arrays.

We can see one example of the screen, which is basically a chain made in 4 blocks.

And there are few values given in all 4 blocks.

Let’s suppose that these are marks.

In one subject there are 90, in second there are 80, then 56 and then 100.

So, we have got 4 subject marks.

Now, how will we tell them that the block with the 80 marks, we have to use the value from there or the block with 100 marks, we have to use its value.

How will I know it?

So, in this basically, we get the numbering done.

Which means, we give the index values to the entire chain.

Like if we had to make a lot of variables in this way.

So, we would have to remember the names of all the variables.

The best way is, you make an array, in array, all the blocks are basically connected to each other.

What does that mean?

When you access the 1st block, you will get to know from

what is the value stored in the other block and where it is stored basically.

So, in this way, we have made the chain and we have stored our marks.

(06/08)

Its counting or its numbering that is done that basically starts from zero.

On the look of it these are 4 blocks but, their numbering starts from zero.

So, we will denote the 1st one with a zero, the 2nd block with 1

, the 3rd one with 2 and the 4th one with 3.

So, whenever I want the 4th block, what I will do is in the

array, the block which is there on the 3rd index position, I would know the value that is inside it.

Third index position means since it is starting from zero.

The compiler will straight away go to the 4th block and it will give us the

value.

So, in this way our array works.

And we don’t have to make so many variables but if the data is of the same type, we just make one array and we tell the size of the array, whether I have to store 10 numbers or 100 numbers.

According to that, the array makes that long a chain.

And in the chain, in each block he takes the input of the value.

Now, we will go ahead and we can see ahead of this chain that we had made, its 1st element is 90 and the last element was 100.

And the 1st element as I said starts from zero.

If we tell the array that we want the zero position, which means we want the value of the zeroth index position.

Basically, what it will give us, it will give us the 1st element.

So, this is the structure of our array.

In which, way it is used.

All these things we have seen.

After that the topic comes to Contiguous Memory Locations.

We had seen that our hard disk that is there, in that in different addresses the variables values get stored.

But what happens in the case of an array? Like I said, that I want an array with 5 blocks which means I want an array with 5 sizes.

In that there will be 5 blocks.

Right? So, how are these blocks connected to each other?

They are connected in such a way that our first block, the next block to it gets stored in just the side location in our hard disk.

In this way, our flow goes on, after the 1st block the 2nd block that is there, that will

be stored just on the next location.

And the 3rd block will be stored on the next location of the 2nd block.

In this way, in our hard disk this gets stored in the contiguous memory allocation, our array variables.

That is the reason why we get to know what the number is, after this and after this.

When we tell our compiler that we want this index position’s number.

We have to get it extracted.

Our compiler understands it and it calculates the particular value and it gives us the value that is stored in that particular variable.

So, somewhat in this way, our arrays work.

We will go ahead and see our most important step, which is declaring an array.

So, we learned what an array is and why it is used.

Now, we will see how they are exactly made.

We talk about the declaration, how an array can be declared.

In declaration, we specify the type of the element and size of the element.

When we declare an array.

In that, we are telling 2 main things.

The 1st thing is that in that array whatever values would be stored, they

would be of which type, we saw that they would be of the same type.

But what would be that type that we would have to tell in the declaration of the array.

So that our compiler gets to know what type of values are stored in it.

So, we have to tell which type of element it would be.

Along with that, we also have to tell what would be the size of the array.

Like we saw that we had to store 5 subject marks.

I would say that its size would be 5.

In this way, if we had to store 100 subjects’ marks, I would say that its size is 100.

This size funda that is there, along with that the data type that would be there of that particular value.

Both these things we have to tell in the declaration to our compiler.

clear up till now,

(10/43)

Now we will see its syntax.

In the syntax 1st will come the data type.

In the same way, how we make a variable.

First comes the data type and after that comes the name of the array.

We can give the array’s name in the same way, as we give any function’s name or a variable’s name, till the time it fits in the rules and regulations of an identifier.

So, we have named an array.

After that comes the most important part of giving the size, which we do in square brackets.

You can see in the syntax that there is a square bracket given.

And inside that, we write the size of an array.

For example, we have made an array of the name R O L L, roll.

Whose data type is integer and in its square brackets, we have written 20.

It means that the array will be of 20 blocks, its size will be 20.

Which means, that there will be 20 roll numbers that will fit into it.

So, in this way, we have to tell our compiler what our array of what type and what will be its size and how it will be written.

So, this is called an array's declaration or declaration of an array.

We have seen array declarations.

Now, we will see how the values are entered in the array.

We call this initialization of arrays, initialising the arrays.

Here what do we do?

“We can initialise the array in C either one by one or by using a single statement”.

In our arrays, we can put the values in 2 ways.

We know that in our array, with the index position, we get to know what will be the variable’s number.

We start with zero and then 1, 2, 3, 4…like its size is N.

Our index position will go on till N minus 1.

So, in this way, with the 2 basic ways, we can initialise our array.

One is by one by one and the other by single statement.

So, we will see the example, so what we have done here is we have made an array named balance.

Whose data type is, double and in it for now there is nothing written in the size, it is empty.

So, this is allowed in an array when along with it, you are writing a number of blocks or number of values.

So, in this example you can see that, after writing equal to in front of it, in the curly brackets you have written the 5 values.

Which means 1000, 2, 3.4, 7 and 50.

So, I have written these 5 values in front of my balance array.

And its size I have not written in the square bracket but that’s allowed.

Why?

Because looking at it we get to know that there are basically 5 values stored in it.

So, its size will also become 5.

So, our compiler understands this by default.

But in one line if you are not putting the value in the arrays.

So, you will have to write the size, you cannot leave the square brackets empty.

So, in this way, the arrays get the initialization done.

This was about a single statement.

How you can fill the entire array with one statement.

Now the way of initialization is, our 1 by 1 way.

In this way, we know any element with its index position, in any array.

Like if I want the 1st element, I will say that the element on the 0th index position, I want that.

(14/38)

Now, I will say that I want another element from my array.

So, I will say that the element on the 1st index position, give me that.

So, in this way, the index values start with 0 and it goes till N minus 1.

So, if I have to store 0th position.

Which means, if I have put the 1st element, how will I write it?

I will 1st write my array’s name and then I will put square brackets.

And after that I will write inside the particular number of the index position.

So, here in the example you will see that we have written balance, it is array’s name.

After that, in square bracket 4 is written.

So, 4 is showing here the particular index position, it is pointing.

How do we get to know that?

If along with the initialization we are also doing the declaration.

In declaration, even the data type also comes ahead.

In this example, you cannot see the data type.

So, what is there in the square bracket?

This basically denotes index position.

If our data type was along with it, it would have denoted the size.

So, if the data type is not here, so in the square bracket, our index position will be defined.

And here you can see that the 4th index position means in the 5th element on our array.

Because it starts with zero.

So, you can see 0, 1, 2, 3 and then 4.

What are we assigning on the 4th index position?

fifty point zero, so in such a way our initialization can happen one by one also.

I will write balance and, in the bracket, I will write 3.

So, 3 is putting some value here.

So, in this way, the declaration and initialization of an array.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

 In the next video, we will be discussing how to access the array element.

The value is stored in the array but how to access them or how to extract them when we need them.

We will be discussing that in the coming videos.

So, till that time you keep practising.

We will meet in our next video, with our accessing the array element’s topic.

Till that time, thank you so much.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere