C Programming Tutorial 99 - Printing Structs, Struct Arrays and Pointers

Sharing buttons:

if we wanted to create a boating plan

down here it would look like this we

would give it the tight building plan

give it a name and then inside of these

curly braces we could just assign a

value for each one of the things Kaleb

Curry for the owner and then for the

rectangle we're actually going to use

another set of curly braces and we could

say this is at a ten by five house or

let's go five by ten just to be

consistent with this here and this is at

position 30 to 48 which I know means

absolutely nothing here but just just

follow along pretend these are some kind

of coordinates of some sort and we can

actually print out this entire house and

we only have to use this my house

variable rather than keeping variables

for all those information so I'm going

to write out a printf statement it's

gonna be a little bit more complex than

usual so I'm just gonna skip to when

it's done all right so here's the format

string the house at position position

size of size size is owned by name then

we just have to make a list of all the

arguments also passed in for these

formats all right so this is the entire

printf statement you can see we can

traverse through these struck Susan the

dot operator so we can say dot position

and then inside that dot X all right so

let's compile and see what happens when

we print this all right I have an issue

all right I have a spelling mistake in

position so I'm gonna scroll up and fix

that I also forgot to put the type here

cuz I'm a failure

apparently let's run the thing the house

at 3248 size 510 is owned by caleb curry

awesome so that shows how to use structs

in your program the next thing i wanted

to show is that you can actually make an

array of structs so for example if we

wanted to make an array to store a bunch

of positions for example it could be a

path we could say path and set that

equal to numerous positions so inside

here each one's going to be inside of

curly braces and we could say where you

need to go for this path so what what

each point is we could then iterate

through this entire array with a for


typically if I'm going to use a number

here I like to assign it to a variable

and then we can basically print these

values like so alright let's see if that

works obviously you don't want to return

beforehand you wanted to do that after I

always forget to do that for some reason

all right so there it will print out the

path awesome another thing I wanted to

talk about is how to make a pointer to a

struct it's fairly simple so if we have

a struct so scroll up we have these this

is my house this is a struct well we can

make a pointer to that just like we

would make any other pointer we use the

asterisk symbol and then give it a name

just like so and then we can set that

equal to the address of my house ok so

when you want to access the values at

this pointer it's a little bit different

so typically you use this dot operator

such as right here but now you're going

to use an arrow

so you could say struct pointer arrow

and then you would be able to go through

here and say position or rectangle or

owner so let's just say position doglegs

and let's print this

you can see there that it prints 32

which if we scroll up we can see that we

did define as 32