Simple Code Highlight of the Week (C++) — typedef

<Blake†Codez />
2 min readSep 17, 2021

What is typedef? Typedef gives us the ability to shorthand variables and types as we code in C++. Let’s explore some examples of what this is and how it is used.

Photo by Fotis Fotopoulos on Unsplash

What does typedef actually mean? Put simply, how it sounds — type definition. How can we use this? To describe simply — we can take a variable or data type and give it a new name to use in the code and reference. An example of this would be a struct. Let’s define a struct called Person but instead of using Person to define a new struct each time we access it, let’s call it p. The general syntax of typedef is as follows: typedef (type) (name):

As you can see I declared the type first which is the struct Person followed by the name I wanted to give this type, which is p. Now I can use this as follows anywhere in my code:

p became my type, or shorthand version, instead of having to type out Person each time. I could also do this to declare other data types and variables. Let’s look at a simpler one. Let’s give a name to an unsigned int using typedef and reuse this in our code again:

Put simply typedef is a good way to create references to data types, variables and structures that makes sense to you and help with your code!

Thanks for checking out my simple code highlight of the week! Please connect with me via LinkedIn at: https://www.linkedin.com/in/blakewoodjr/. You can also find me on the Flutter Community Slack channel as blakecodez.

--

--

<Blake†Codez />

I’m a Software Engineering student in Redding, Ca. Love all things Computer Science related, love for journalism, Jesus Christ, and team collaboration projects.