C Programming Tutorial For Beginners

Be looking at how we can use a struct to represent a student inside of our program so imagine we were writing a piece of software that was using like students so it was like storing records of students or something I could use a struct to represent a.

Student in my program so over here in my program I’m just gonna come up here above the main method and I’m actually gonna create a struct you guys will see like how these work and how we can use them so I’m just gonna say struct and over here I’m gonna give this a name so I’m gonna call this student and a lot of times in C when we’re making structs you’re.

Give them a capital letter just like that and I’m gonna make an open and closed curly bracket now inside of this struct I can start specifying what types of data are gonna make up a student in my program so basically I can define like different attributes of a student and place them in here and this will kind of act as a template and you’ll see how we can use this later so let’s.

Think about different attributes of a student well one thing would be like the student’s name so I could say char name and I’m gonna make this a array of characters so this is gonna represent the name and actually why don’t we give this a number so I’m gonna say 50 so this string can hold 50 characters maximum and now we’re gonna make another one for major so we’re gonna want to.

Store the students major and again we’ll make this 50 I also want to make an integer for the students age and a double for the students GPA so I have this struct student and in here I have.

After buttes of a student right have like the students name their major an integer that stores their age and a double for their GPA essentially what I did was I created like a type.

Of student data type so I basically am allowing myself now to represent a student inside of.

My program so let’s come down.

How we can use this so I can basically create an instance of this student structure so I can create like an actual student inside of my program and.

The way that I do that is just by saying.

Struct and I want to type out the.

Name of the struct that I want to make in our case it’s gonna be a student and I want to give this a name so I’m just gonna call it student 1 and I can just use a semicolon here basically what I did now was I created a container called student 1 that’s gonna be able to store a name a major in age and a GPA so if you’re familiar with arrays in C you’ll.

Know an array is a special structure that can hold.

Multiple pieces of information but all the pieces of information inside of an array needs to be of the same data type and also they don’t have names with a struct I can have a bunch of different data types like this and I can also give them names like name major age and GPA so now let me show you guys how we can assign some values to these so for this particular student student 1 I can give.

Them a name I can give them a major a GPA and an age so I could say student 1 dot age and I can set this equal to something so I.