Could say let’s say student ones age is 22 so basically inside of this student 1 container I’m.
Saying the age of this particular student is gonna be 22 I can do the same for the GPA so I can say student GPA is equal to maybe they have a 3.2 so I’m saying this particular student’s.2 I can also do the same thing for those strings so for the name and the major now here’s the thing about working with strings remember in C a string is actually just an array of characters and when we have an array we can’t like give it a particular value.
So for example if I wanted to give this students name of value this is just an array of characters so I can’t just come down here and say student name is equal to something that’s not gonna work because you can’t do that with an array I can use something called the string copy function.
Is a function that will take a string and it’ll give it a value that we specify so it’ll basically do what we want to do like what we did over here with the age and.
The GPA but with the name so I could say like str c py and inside of here i need to pass this two parameters the first thing i want to pass is the destination for the string so we’re gonna pass in student 1 dot.
Name the second thing i want to pass in is the string that i want to store inside of student 1 name so in our case let’s just name it him Jim so now we have.
The student 1 names value is equal to Jim and I can do the same thing for major so I’m just gonna copy this and we’ll come down here and I could say student 1 major and we’re gonna set this equal to business so let’s say Jim is a business major essentially what I’ve done here is I’ve created a student and that student had all the attributes that we defined up here so this particular student had a name a major in age and a GPA and I.
Gave all of those values so I assigned this student 1 an age of 20 to a GPA of 3.2 etc so now what I can actually do is I can print out.
All these different values that are stored inside of this structure so if I wanted for example I could like print out the GPA so I could print out student 1 GPA and now we’re gonna be printing out 3.
Actually it looks like I have a typo here this should be student 1 not student alright so here we’re printing out 3.2 so we’re printing out the value of the student’s GPA I can also print out like their name so why don’t we do that student 1 name and now this is gonna print out so you see we prints out over there so a struct is a really useful structure and another cool thing we can do with structs is we could actually create another student so I could create like another.
Instance of that student structure so I’m actually just gonna copy all this stuff I’ll show you how we can do this I’ll just come down here and paste it and instead of student 1 why don’t we call this one student 2 and we can set this student’s age to be like maybe 20 maybe their GPA is a 2.
Say their name is Pam and they’re studying art so now I have a completely different student and if I wanted I could print out this students attributes so I can print out like student 2 dot name and now we’re gonna get Pam instead of Jim so you.