Getting Started with Programming – Part 8: Typedef and Structs

Previous parts of the Getting Started with Programming series:

This will be a C/C++/Arduino based blog post. Python has a related mechanics to those we’re going to take a look at here, but we’re going to skip the Python-examples for now.

Typedef

The first topic of this blog post is the so called typedef.

What?

typedef is a keyword in C and C++ which lets you create custom data types, or more accurately: create an alias name for another data type. Keep om mind that it does not create a new type, but instead adds a new name for some existing type.

Why?

Sometimes you may want to use data types that are relatively complicated to write out, perhaps with some intricate pointer syntax thrown in there for good measure. With typedef you can create a short and sweet alias for this data type, for instance bacon, and then write bacon instead of the complex data type each time you declare a variable of that type. This can lead to simpler coding and better readability and documentation.

typedef can also help with portability of your code where data types are machine-dependent. When moving your code from one machine to another you just have to change the typedefs instead of every variable declaration line where the data type is used.

How?

The syntax for typedef is really simple!

typedef int bob;

bob my_variable = 0;

The above code is silly, but it explains the syntax in its most simplistic form. bob is now a different name for int. my_variable is a variable of the data type int. Then why use typedef at all in this code? Good question! That’s why the code is silly.

Here’s a similar, but bit more useful code snippet:

typedef unsigned long *ulongptr;
   
ulongptr a;

The above code makes a an unsigned long pointer variable.

You can craft much more complex data types as well, which just increases the convenience of typedef.

Struct

The second topic of this blog post is the struct.

What?

A structure aka. struct is a way to group variables together, possibly of different types. One can have several instances of a declared structure. The variables within a structure are called members.

Why?

In large programs, structures can help you organize complicated data in a way such that a group of related variables gets to be treated as a unit. By doing this, your code quickly gets more structured (pun intended).

How?

struct point{
	int x;
	int y;
	float z;
};

struct point my_point1;

struct point my_point2 = {2, 5, 3.7};

my_point2.x = 4;

The first five lines in the code above is a structure declaration of a point in a three dimensional space with three coordinates as members. The Z-coordinate is of type float just to illustrate that we can have several different data types within a structure.

Line 7 creates an unitialized struct point variable called my_point1.

Line 9 initializes a variable called my_point2 with the x, y and z values specified in the curly brackets. The order here is important and corresponds to the order of the members in the declaration.

Line 11 changes the x member of my_point2 to 4. Notice the . operator. This lets you access members of a structure variable for both reading and writing.

Typedef and Structures Combined

Many tend to use struct and typedef in tandem like this:

typedef struct{
	int R;
	int G;
	int B;
} color;

color LED_color = {255, 0, 0};

One advantage by doing this is that you don’t have to write struct every time you declare a variable of this type (like we did in the last chapter on code snippet line 7 and 9). Some consider this a bad practice since a lot of typedefing can clutter already busy namespaces in larger programs. This is however rarely an issue with small to medium-sized projects.

Summary

This blog post has mainly been about convenience and readability. Even though these concepts might at first seem like mere fluff to tidy up your code, they are important to master.

We’ll for sure revisit this series some time down the road since there are still plenty of material to talk about!

Related Posts