Structs are basically user defined data types that allow you to encapsulate multipe variables under one "super variable." Think of it as being able to have my name, age, sex, zip code or whatever, all under the variable "me." That is what structs are. You simply create a template using existing data types, create an instance of that template and fill in the available blanks. The only limitation is that each instance must be the same size in bytes, which pretty much only means that you can't have an unsized array declared within a struct. While structs can be declared anywhere (even in a place with limited scope, such as within a function), for most purposes you'll simply declare it at the top of your program next to your function prototypes. Here is what they look like.
struct Person
{
char name[500];
int age;
};
As you can see, they all begin with the keyword struct, followed by the struct's name (alphanumeric characters and underscores, only!) and then brackets containing whatever variables you want the struct to contain. Note that you don't initialize anything here -- this is only a template which we'll fill in later. Also note that you can have arrays; you must define a size for them though. Finally, remember the semicolon after the struct!
Using a struct is simple as well. After it has been declared, simply use the struct's name as a data type and create a variable with it. To get to the variables it contains, simply use the variable name you assigned it, followed by a period (.), followed by the variable you're looking for as its named in the template. You can then assign it values as you would any other variable. Here's an example using the struct defined above.
Person me; Person him; strcpy(me.name,"Ryan Boyer"); strcpy(him.name,"John Doe"); me.age = 21; him.age = 33;
As you can no doubt see, you are allowed to create as many instances of this struct as you wish; the data they contain does not overlap in any way.
That is about all there is to say about structs, so rather than end the lesson early I'll go ahead and expose you to a method that you'll use a little more extensively later on. Please assume that the struct above still exists.
struct HouseHold
{
Person member[4];
char street_address[500];
char city_state[100];
int zip_code;
};
This introduced not only an array of structs, but a struct within a struct. Usage is probably exactly what you'd expect. Again, assume all of the code above still exists.
HouseHold myHome; myHome.member[0] = me; strcpy(myHome.member[1].name,"Sister Boyer"); strcpy(myHome.member[2].name,"Mother Boyer"); strcpy(myHome.member[3].name,"Father Boyer"); myHome.member[1].age = 24; myHome.member[2].age = 45; myHome.member[3].age = 46; strcpy(myHome.street_address,"123 Fake Street"); strcpy(myHome.city_state,"Jacksonville, FL"); myHome.zip_code = 32255;
The first thing you'll see is that you can set structs of the same data type equal to each other. As for structs within structs, you can see that you simply "climb" down to the variable you're trying to invoke.