declare

C Programming Tutorial - 68: Array of Structure Variables



Sharing buttons:

hello guys welcome to my channel this is

the 68th tutorial in this course and in

this tutorial we are going to talk about

array of structure variables now in the

last tutorial we saw how we can declare

structure variables inside the main

function and then access the members of

the structure using the member access

operator and put values in the structure

members and then later on display those

values on screen that's pretty much what

we're going to do in this tutorial as

well but instead of declaring two

different variables in the main function

we are going to create an array of

structure variables and you're going to

see how that simplifies a lot of things

for us so as you can see using code

blocks I have saved the file at give me

the name array of structure variables it

has the extension dot C on line 1 in

this file I have the stdio.h header file

on line 2 I have the string or a char

file and then on line 3 I have declared

a structure called album and it's the

same structure with which we've been

working in the past so it has four

elements in it and all four of them you

can see now our arrays of character

characters right so title is an array of

character or you can think of all of

these as strings right so title is a

string that can have a maximum of 35

characters and then artist is another

string that can again have a maximum 35

characters in the number of tracks and

here

both of these are also strings and the

reason why I have declared these strings

and not as integers is because we are

going to use the gets inputs functions

to work with these two elements as well

right so then on line number 10 I have

declared the main function inside the

main function you see that there is a

declaration statement that declares

structure variables but instead of

declaring two variables as I told before

we are going to work with an array of

structure variables so I have to use the

keyword struct first and then the name

of my structure which is album write and

then I have to specify the name of the

array of structures that I'm going to

use so that's album and here the a is

small case and then within square

brackets I can specify how many

variables do I want

right so in this tutorial we're going to

work with two because you know these the

code that is going to follow the

Declaration is

is pretty long right so I mean although

I would have to repeat the code for the

variables that I have but still you know

to process all these structural

variables it would require a lot of time

and to avoid that I've just declared an

area of two variables and then on line

number thirteen I have declared two

integers and they're called I and J and

then on line number fourteen I have used

a for loop to loop over the code that's

there between the curly braces on line

number fifteen and line number 25 we're

going to see that code but the reason

why we've used an a loop to go through

the code is because we've use an array

here right so in order to access both

the variables inside this array we would

have to use a loop right so the initial

value of I I firstly is the control

variable for the first for loop and the

initial value of it is zero and it's

going to loop till one right so they're

going to be two iterations of the code

between the braces and then on line

number 16 as you can see there's a

printf statement that's going to display

a message enter title on screen and then

I've used the gets function to get the

value for the title element for the

first variable inside decided try to the

first variable will have the index 0 so

that's why album within the square back

which you have I and the value of 5 for

the first iteration is going to be 0

right so album 0 dot title would put the

value inside the title element for the

first variable in this I of structures

then on the next line I have another

printf statement that's going to display

the message enter artist and then I've

used the gets function to get the artist

name and then I have used printf to

display the message enter number of

tracks and I've used the gets function

to get the number of tracks for the

first variable and then printf says

enter ear and then I have again used you

know the gets function to get the value

for ear right so this entire process is

going to be repeated one more time when

the value of I is 1 right so for the

next for loop iteration that is and then

you must notice that I also have a

printf statement on line number 24 that

is not going to display anything it's

just going to you know print some

characters on screen and it's it's

actually go

to execute twice right and once after

each for loop iteration and it's it's

just to make things need right so then

online were 26 I have another printf

statement that is going to display the

message displaying on from information

because now we've received information

from the user right and now we would

want to display it on screen so we're

going to do that using another for loop

and this one's going to use J as the

control variable and you can see that I

have given J the initial value 0 and its

maximum value is going to be 1 so again

they're going to be 2i trations for this

loop as well and in this loop 2 I have a

series of printf statements and I've

used the puts function here because

instead of getting information from the

user we're displaying stuff on screen

right so on line number 29 there is a

printf statement that this place title

with a colon and then you know I've used

a puts function to display the value of

title and then printf stay says artist

and then I've used the puts function to

display the artist name and then again

you know there are pairs of printf

statements and puts function statements

for number of tracks in here right in

also I have a printf statement on line

number 37 that's going to display the

astrick symbols and obviously it's going

to split lines right so let me run this

program I'll have to click on build and

run to do that and there you go in the

output window I see the message enter

artist right so if I type in an album

name Dark Side of the Moon and press the

Enter key I see the next message which

is enter artist if I type in pink

floyd--

press the Enter key asked me for the

number of tracks type in nine press the

Enter key asks me for the earth

I live in 1973 here and then you see

that um you know after a line break I

see those seas of astok symbols that I

included in the printf statement that I

had in the main function in the for loop

and then I am asked for draw the title

again right so this is because we had

two structured variables so for the

first

when we've entered for the second one we

are going to enter now so I'll type in

wish you were here and then again the

artist is Pink Floyd and the number of

tracks in this album the five and the

year in which the album was released was

1975 right and there you go

when I press the Enter key I see that

after a line break

I saw the astral symbols again and then

there was another line break and then I

see the message displaying information

and then the information that I supplied

as input to the program that information

is displayed right so I see the title as

Dark Side of the Moon are the spring

flow and number of tracks and you know

again the same thing for visible here

so that's it for now in the next

tutorial we are going to check out

another feature of structures maybe so

please subscribe my channel in case you

haven't already and I'm going to see you

soon