In this tutorial you will learn about C Programming - Structures and Unions, initializing structure, assigning values to members, functions and structures, passing structure to functions, passing entire function to functions, arrays of structure, structure within a structure and union.
Structures are slightly different from the variable types you have been using till now. Structures are data types by themselves. When you define a structure or union, you are creating a custom data type.
Structures in C are used to encapsulate, or group together different data into one object. You can define a Structure as shown below:
Structures can group data of different types as you can see in the example of a game object for a video game. The variables you declare inside the structure are called data members.
Structure members can be initialized when you declare a variable of your structure:
The above declaration will create a struct object called player1 with an id equal to player1, xpos equal to 0, and ypos equal to 0.
To access the members of a structure, you use the . (scope resolution) operator. Shown below is an example of how you can accomplish initialization by assigning values using the scope resolution operator:
Since structures are of custom data types, functions can return structures and also take them as arguments. Keep in mind that when you do this, you are making a copy of the structure and all it's members so it can be quite memory intensive.
To return a structure from a function declare the function to be of the structure type you want to return. In our case a function to initialize our object structure might look like this:
Let us now learn to pass a structure to a function. As an example let us use a function that prints members of the structure passed to it:
For completeness we shall include the full source of the above examples so you may see how it all fits together.
Here is what the output looks like:
Since structures are data types that are especially useful for creating collection items, why not make a collection of them using an array? Let us now modify our above example object1.c to use an array of structures rather than individual ones.
We create an array of structures called gamobjs and use the createobj function to initilize it's elements. You can observer that there is not much difference between the two programs. We added an update for the enemy1's position to show how to access a structure's members when it is an element within an array.
Here is the output for this version:
Structures may even have structures as members. Imagine our x, y coordinate pair is a structure called coordinates. We can redeclare our object structure as follows:
You can still initialize these by using nested braces, like so:
To access or set members of the above internal structure you would do like this:
You simply add one more level of scope resolution.
Unions and Structures are identical in all ways, except for one very important aspect. Only one element in the union may have a value set at any given time. Everything we have shown you for structures will work for unions, except for setting more than one of its members at a time. Unions are mainly used to conserve memory. While each member within a structure is assigned its own unique storage area, the members that compose a union share the common storage area within the memory. Unions are useful for application involving multiple members where values are not assigned to all the members at any one time.
Let us modify our structure object from above so that it has a union for indicating dead or alive in it:
Only dead or alive can be set to anything at any one time. You can get to it the same as with a structure inside a structure as we learned in the last section.