declare

C Programming Tutorial - 49 - Structures



Sharing buttons:

alright guys so my boss just came to me

and he asked me for a way to print out

all the information about every employee

in our company so we have 500 employees

and they each have a user ID a first

name a last name and we also store their

age and their weight because while my

bus is where you wants to make sure um

you know no one's eating too many

cheeseburgers so we also keep track of

their weight so this is what I did I put

int user ID and then I put char to make

a character array first name and no one

has a first name longer than 25 I also

did this for their last name and I also

needed a store and print out their age

so that's an integer and their weight

which is a float alright so this can

store all the information for user 1 and

now let me just do this for user 2

okay okay oops oops messing up there

your - - you know what if I do this for

500 freaking people it's going to be a

pain in the butt so I'm scratching my

head and I'm thinking would it be cool

if we could just make this template one

time a template that could store the

user ID first name last name agent

weight and we can use it over and over

and over again I know what I can do I

can use an array is you can store a

bunch of crap in there okay well wait a

minute an array can only store a single

type of variable for example it can only

store in or floats you can't store an

INT and a char and a float in the same

array ah crap so we can't use that well

ladies and gentlemen welcome to the

wonderful world kind of a tongue twister

of structures a structure is a way that

you can group a bunch of variable types

together and they don't need to be the

same type for example you can pretty

much create a blueprint for a person and

store an INT a care and a flow all

together pretty freaking sweet so let me

show you

had to do that and you guys are going to

love me after this now I want to mention

one thing whenever you make a structure

you can just go ahead and make it up

here or wherever you want but 99% of the

time programmers like to make their

structures in a different file

it just helps organize everything and

it's just I don't know comment amongst

programmers so they put them in a

separate header file and maybe remember

that we made this header file like I

don't know 20 tutorials ago or something

so open that back up first of all cut

this because we don't add any of our

structured definition in the main file

so now let me show you guys the basic

syntax of how to create a structure

whenever you create a structure the

first thing you write is struct pretty

much they'll see hey I'm going to make

any structure the next thing you do is

you need to name your structure and this

is just so if you have like a structure

to store employee information and if you

have another structure to store I don't

know student information maybe teacher

information well if you have multiple

structures you're going to need to be

able to identify them so we need to name

it so after this what you do is add two

little curly braces and a semicolon the

semicolon just says we're done with the

structured now in between those curly

braces is where we write all of those

individual variables these are called

members of the structure so a structure

is pretty much just a group of variables

and those individual variables are

called members simple enough so now

basically as I'm going to be showing you

guys later on what we can do is we carry

a bunch of users and they can each have

an integer character for their name all

of this crap and we just group together

a bunch of variables of different types

pretty freakin sweet and if you're ever

wondering well what's this useful for

it's really useful whenever you're

working with um you know like users or

employees and you want to like display

all of their information at once as a

table or anything like that so I'll show

you guys how to actually use this

structure right now

now that

we made it might as well use it right so

of course since this is in the header

file the first thing we need to do is

import this so or include it I should

rather say I'm use the program Python

anyways include and remember we can just

use those double quotes

Bucky's info H and what this does is

essentially copy this and paste it right

in here it does the same thing even

though you know it's going on behind the

scenes so now we have that structure

included so let's use it right now so

whenever we use a structure the first

thing we need to do is remember we can

store as many people as we want using

the same template so how do we sort each

individual person well just say that

we're going to be using the structure

what when are you going to be using what

were you going to be using the user

structure that we just created and after

this you write like the name of the

person so we'll say for this one for

this template we're going to do it for

Bucky and you can do this for as many

people as you want now we're going to

say okay let's do it again and let's use

another one for Emily okay simple enough

so if you're saying okay so Bucky now

has a user ID first name last name aging

wait Emily now has user ID first name

last name agent wait a lot faster than

typing out you know uhm user ID one two

three four

save this a whole bunch of freakin code

but how do we actually set Bucky's

weight to something or set Bucky's user

ID to something well in order to do that

you do this type the name of whatever

you named it up here so Bucky and then

after it hit the dot and this is a new

operator that I want to tell you guys

about this is called the dot operator

and it's used for accessing individual

elements or items inside your structure

and again they're called items or that's

what I call them but technically they're

called members so user ID as a member

first name is a member so this is how we

says each member or variable inside the

structure

so say to set Bucky's user ID just put

Bucky's dot operator user ID and just

treat it like a normal variable so

what's my user ID I don't know we'll set

this equal to one so whenever we're

setting the user ID of Emily Emily user

ID course she doesn't want one she wants

to so on and so forth now after this

again we don't just need to set it

explicitly using the equal signs we can

do cool stuff like puts so just give

them a little prompt will allow the user

to run this program and like enter their

first name or something I don't know so

enter the arm first name of user 1 and

on this okay actually that's good

I'll keep this program simple and of

course whenever we do gets just like

before we put Bucky first name and

typically just just feel like user 1 and

user 2 but just saw so I can like really

easily display what's going on or just

put Bucky first name you know a little

bit easier now of course we can do this

for another the first name of user 2 of

course that would be Emily's first name

so whatever name they enter right here

get stored in the first name of Emily

and Bucky tomato tomahto

simple stuff so I guess now that we know

how to access the members of a structure

how to make a structure might as well

just go ahead and print it out get this

story over oh so we can move on to

something a little cooler actually so

put like

okay these are one can't type I'll print

out

Bucky's ID user one ID is percent a and

I'll keep that tuned to the line who

cares so ID is bookies user ID

and copy this

alright so users - let's see first name

is that would be a percent s because of

the string and that would be Emily's

first name which is hopefully Emily all

right and that's pretty much it so now

when I run this bad boy come on there we

go

all right so enter the first name of

user one well that is Bucky enter the

first name of user - that's Emily check

it out

user one which is me my ID is one these

are - Emily her first name is Emily

simple stuff so again this isn't really

useful in this sample because right now

our program is really small it only has

two users however when you have uhm

bigger programs and when you're actually

working for a company what's going to

happen is instead of having to type

these variables 500 times for each user

you can just make one single structure

and it can be a blueprint for every

single user so then you can do just cool

things and loop through in like a print

out all of their items and database or

in a CSV file or whatever you want to do

but now you know how to use structures

and since we know how to use structures

we learn about memory in the next

tutorial I want to start talking to you

guys about files how to create a text

file programmatically and some really

cool things that you can do with them so

thanks for watching remember to

subscribe and how it will see you later