Programming in C: Typedef

This article is part of a series – How to Program Anything: C Programming

Preface

Now that you know how to to use basic data types, and aggregate data types such as a structure or union, it might be handy to know how to use them more easily in your code.  However, what we’re about to discuss isn’t limited to making structures easier to use, it also can be aimed at making code more portable across machines.  Whereas, when you port your code to another platform, had you specified the exact type of each variable that might need changed when that variable was declared, you’d have a lot of variables to change.  What if ther was a way to name a certain type, and then use that name across your program?  That way, if that particular type needed to be changed, say to be an int instead of a char, you could specify that in one place.  Well, that’s where the typedef  statement comes in.

The Typedef Statement

We’re not really to the point of studying statements yet, but typedef is so tied into data types, both basic and aggregate, that not talking about it in a discussion of data types would be leaving an important piece out.  At its core, a typedef statement defines an alias for another type.  When you use the typedef statement you are not actually creating a new data type, you are simply creating an alias, or a reference, to an existing data type that you can use in your code.  Let’s look at an example of creating an alias for the float data type:

In this code snippet we’ve created an alias called rate for the float data type.  Now we can define a variable using the rate alias, and it will really be a float.  We’ve told the compiler that rate is another term for float, and that’s all.  We haven’t actually create a new type.   Let’s make a waterpump rate variable:

Waterpump in this example is a float as we’ve defined, but in the code it’s defined as rate.  Now we get to what I was talking about in the preface.  Say we are porting our code to another platform, and we discover that everywhere we defined a float for a rate, we actually need a double. Well, by using a typedef we can make an adjustment in one place in our code, and it will update to all the places we used that typedef.  In this case we’d change typedef float rate;  to typedef double rate;

NOTE: There is a caveat with typedef of basic types.  Say I make a typedef of an int: typedef int number; I can then use this in my code as a type of number, however it’s not a clear inline substitution for the int keyword.  If I tried to create a variable like so: unsigned number myVariable; the compiler would complain.  The number type I’ve defined doesn’t have the ability to be modified by the size modifiers that an int has.  If I wanted to define a number to be unsigned, I’d have to do it in the typedef statement.  When you make an alias of a basic type you lose the ability to make size modifiers on the type.

As you can see the general outline of a typedef declaration is:

Typedef With Pointers

It is actually possible to “embed” the type to be a pointer as well.  If we wanted a particular type to always be a pointer, we’d just provide a point as the type to be aliased.  This is done like so:

In this case int_ptr would be an alias for int * or basically a pointer to an int data type.  If we create a variable of type int_ptr, at its base it would actually be an int *.  This can build upon itself as well for example, say we declared: int_ptr *myHandle; In this case myHandle would actually be of type int **, or a pointer to a pointer.

Typedef With Aggregate Data Types

That is one handy use of typedef.  Another handy use of the typedef statement is to make the use of aggregate data types simpler.  For example, you can use a struct in a typedef statement to create a new alias data type like so:

In this example, we’ve defined a structure with the tag Address.  If we wanted to make a variable of this structure we would normally have to type struct address myAddress;  But with typedef, we can shorten that down to a particular alias, in this case simply address .  Note that I made the structure tag with a capital A and the type name with a lower-case a.  In C, case matters and these are considered two different identifiers.

In fact, we can combine the struct definition and the typedef into a single statement like so:

In this code, I defined a struct tagged Address, but instead of defining new variables afterward, with the addition of the typedef keyword in front of it what would normally be variables become type aliases.  In this example, address becomes an alias for a basic Address struct, and addressPtr becomes an alias for a pointer to an Address struct.  We can actually leave out the tag name for the struct completely and simply use the alias if we desired:

Typedef With Arrays

You can actually also use typedef with arrays.  The format for that is a bit different but a code snippet may illustrate:

In this example we’ve decided that the data type of type name will be a 25 element char array.  Now if we define a variable to have data type of name the compiler will interpret it as a 25 element char array.  So for example, we could write something like the following:

However, say we wanted to specify a pointer to the array itself, but we didn’t necessarily want to change the typedef that’s already been defined.  You’ll find that if you add a pointer to an array alias, you’ll get somewhat the same result as when you added a pointer asterisk to an alias that was already a pointer above:

In this case you have defined a pointer pointing to the memory address fo the char[25] data.

Typedef With Typedef

You can use an alias that’s already been made in another typedef to create another alias.  In this case the alias is of an alias of a data type.  The compiler will always resolve any aliases to the base data type used in the original typedef statement.  You can get into trouble aliasing an alias like this, as too many of them will make it harder to determine what the original data type is of a given variable.  This is known as obfuscation, in that it creates layers that “hide” the original variable type data that a programmer, wanting to know the data type, would have to wade through.

Conclusion

As you can see using typedef can make your code easier to read and easier to maintain, if used with proper discrimination.  However, if you use too many typedefs, as noted above, it can obfuscate what data types are actually being used in the code.  If a new programmer comes onto your code, he’ll want to know what kind of data types many typedefs are and if he has to look up typedef after typedef, it can be very confusing.  However, there are reasons to use a typedef in many cases.  As stated above, using a typedef on a data type that may have to change if the program is ported to another machine enables you to only have to change the data type in the code in one place, rather than every single place that data type is declared.  Also, with aggregate data types especially and with function pointers (we haven’t gotten to those yet), it can make complex and in-depth declarations easier to understand.  It’s easier and more natural for a programmer to use a single alias for a large structure than defining the structure every time.  Typedef’s can be a mixed bag, if used too much you acquire obfuscation in your code, but if used to little, you may end up having to edit your code in multiple places; or you may end up with code whose purpose is difficult to understand.  I hope I’ve illuminated typedef to some degree for you, thanks for reading!

This article is part of a series – How to Program Anything: C Programming

If you appreciate this article you might consider supporting my Patreon.

But if a monthly commitment is a bit much, I get it, you might consider buying me a coffee.

photo credit: Jitney58 Sun Spots 9-7-2017 via photopin (license)

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: