Categories : C


Hang on everyone, this could be a little rough. Just when you thought everything was going smoothly, you’ve come across a twist that doesn’t quite make sense… a struct. Those who come from a Java or a (shudder) C++ background will find comfort in thinking of a struct as a mellowed out version of a class, and you can think about it in that way. But for those of you breaking new ground, it can be a little bit complicated, so I’ll be wing leader on the first run down the trench, and hopefully you’ll get a better idea as to how to shoot the Death Star yourself next time through. Don’t worry, eventually this becomes about as easy as picking off TIE fighters.

The first thing to understand is that a struct is a group. You can use a struct, for example, to give a group of common variables a common identity for a common purpose. You can also use a struct so that you can use the “same” for different purposes at the same time. Lets see if an example can help.

Okay… here’s the deal. I have a whole bunch of screws; different types, different sizes, and different numbers of each. It behooves the purposes of organization to classify these screws by type and size, and to provide a count of the screws. This would be a good way…

Tiny computer screws: 12.
2 inch wood screws: 20.
1 inch wood screws: 25.
1 inch machine screws: 10.

All of these screws are in their own individual container labelled as I have done above. For the coup de gras, I then take those small, individually labelled containers and put them in a bigger box labelled “starX’s screws.” Neat, eh?

So neat in fact that my buddy Zoidial comes along and decides he wants to organize his screws in a similar fashion…

Tiny computer screws: 20.
2 inch wood screws: 10.
1 inch wood screws: 10.
1 inch machine screws: 5.

And then he puts them in a bigger box labelled “Zoidial’s screws.” The organization has taken care of itself, but we have to be more specific because of that. I can’t say, add 10 more 2 inch wood screws, because we have 2 different boxes of wood screws. I CAN say add 10 more 2 inch wood screws to Zoidial’s box of wood screws. In C parlance zoidials_screws.two_inch_wood += 10. That’s the crude example, so let’s look at some code…

#include <stdio.h>

int y = 0;

struct s
  int x;
  int a[5];

typedef struct s gogo;

int demo(gogo try)
  try.x = 1;

   while (y <= 4)
       try.a[y] = try.x++;
       printf("%d\n", try.a[y]); 

int main(void)
 return 0;


Some of this should be familiar to you, so I’m going to skip those parts and focus on the meat of the struct relavent stuff.

struct s { … }try;
Obviously, I’m ignoring the stuff between the curly braces for now. They are, afterall, nothing we haven’t seen before. Generally speaking, what this does is declare a type of struct with files in it (struct s { … }), and then decalres a variable of that type (try;). try is an instance of type struct s. If you want to access one of the fields in struct s, you have to do it through the instance (in this case try). The most important thing to remember is that variables in struct s are treated as variables of the instance of struct s.

typedef struct s gogo
To create a new instance of struct s, we can type struct s try;, but ultimately this proves to be cumbersom. The most common reason for grouping things together in structs is because they have something in common, and so we want to treat them in a common fashion. Thus, it helps to be able to refer to them with a common name. typedef lets us do just that, define a new type with whatever name we want. While you can do it with anything (read more on typedef to find out how), it is particularly handy with a struct. In this case, we don’t have any real rhyme or reason behind the naming scheme, so as an example we use gogo as the name of our type. From this time forward gogo new_instance; is exactly the same as typing struct s new_instance;. Practice this, it will not only save you typing, but it will help you keep things strait in your own mind.

int demo(gogo try)
Here we see the typedef in action. We’re passing an instance of the struct into the function. No biggy.

try.x = 1; Here we see a field of a struct being accessed. Remember, we have to make an instance of the struct to access anything in the struct. In this case, try is the instance of the struct, and we want to access variable x. Remember the syntax is struct_instance.variable_name

while (y <= 4) This is noting we haven’t seen before, but I want to call your attention to something. We declared the variable y GLOBALLY, so we refer to it as normal. Be careful, this one might trip you up.

You’ll notice that I skipped over most of this program because I wanted to focus on the key points of the struct. If you’re having problems with some of the non-struct related parts of the problem, you should probably look at one of the other tutorials first. If my explanation of the structs seems faulty, please send me an email.

 Posted on : August 22, 2014

You might also likeclose