declare

Structs and Pointers to Structs in C -- Engineer Man



Sharing buttons:

engineer man here time to learn about

structs and struct pointers in C no time

to lose let's do this

today's level is beginner and making no

assumptions with this video so first

what is a struct well struct is short

for structure and it's a way of

logically grouping items of different

data types into one variable different

than a single variable which would be a

single data type today we're going to

create a person struct but there's tons

of uses you can store anything in struct

including others drugs start to use just

like any other variable except use the

dot or - angle bracket notation to

access the item in the struct which

notation you use is going to depend on

whether it's a normal struct or a

pointer to a struct let's look at some

examples so create a program here I've

created my or I've defined my struct up

here and I put two things inside it I

put an integer called age and I've put a

character array called name now I'm

going to fill those out here in a second

so let's say you define this drug to

actually use that struct

use the struct keyword you specify the

name of the struct and then you specify

the name of the variable so in this case

we now have a variable called e/m which

is of type struct person to set some

data notice we're using the dot syntax

don't don't come down here just yet

you're gonna see different syntax we'll

explain what that is the dot syntax is

how you assign values to a property in a

struct so in this case I'm saying

EMH an e/m is just short for an engineer

man just something I picked could be

anything EMH equals 32 and now I'm doing

am name and allocating six bytes on the

heap I'm copying the string literal

Brian into e m dot name perfect that's

how you use a normal struct node

pointers involves moving on to pointers

here we're creating a pointer to the

struct so we're doing struct person

again we're doing the star e m pointer

and we're taking the address of the e/m

struct and we're assigning it to the en

pointer variable so now we have a

pointer to strut

this is where the syntax changes

slightly you can still use the dot

syntax but you would first have to

dereference that struct if you watch my

other videos you can learn about

dereferencing so I call asterisk here on

am pointer I'm getting the value of VM

once I've done that I can use dot syntax

no problem but as you can see that's a

little messy I have to dereference it

have to surround it in apprentices

because if I don't going to try to

dereference the whole thing the endpoint

of age and that would be an error and

then to reallocate memory for name I

have to again use that strange syntax

over and over again but this does work

this works perfectly in fact it's it's

identical to this in its output or Intel

coma so looking at what you can do now

if you're using the pointers destructs

is you can use the special syntax this -

angle bracket oh and that's exactly the

same as as this so in this case you can

just do the in pointer - care at age 30

for the endpoint or - carrot name

reallocate am fired - carrot name put 8

bytes on the heap and then put the

string literal Brian exclamation mark

exclamation mark into Ian pointer name

and that's it I want to cover one other

thing if you didn't want to do this type

out struct person you could use

something called a type def and types

tabs or something I'll cover another

video more depth but if you want to

shorten that you could put type def

there you could remove person from there

add it down here and what you're

essentially saying is define this as

type person rather than type struct

person if you were to do that you can

just drop structs off of here and that

would be perfectly valid it's just a way

of shortening it but again I'll cover

that in more depth in another video and

that's it hopefully everyone is smarter

having watched this video if I forgot to

cover something we like to request a

video post a comment below if you

enjoyed this video

smash that like button

ah forget subscribe so you don't miss

new videos see you next time