ArticleslgStudy

science

Typedef

Typedef is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Typedef rather than just read about it. In short: typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

Key takeaways

  • Typedef belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Typedef to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Typedef from memory before moving on to harder problems.

Reference excerpt

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.

Syntax A typedef declaration follows the same syntax as declaring any other C identifier. The keyword typedef itself is a specifier which means that while it typically appears at the start of the declaration, it can also appear after the type specifiers or between two of them. In the C standard library and in POSIX specifications, the identifier for the typedef definition is often suffixed with _t, such as in size_t and time_t. This is practiced in some other coding systems, although POSIX explicitly reserves this practice for POSIX data types. In C++, this _t suffix is not used for typedefs (as C++ does not mandate prefixing structs with the struct keyword), but does appear in some types in the C++ Standard Library (especially for tag types, like std::in_place_t) along with other suffixes like _v (typically representing an abbreviation for a T::value field).

Examples This snippet declares the type Length as an alias of the type int, allowing a user to use Length as a type in declarations where they would otherwise use int:

Documentation use A typedef declaration may be used to give more contextual meaning than is given by the underlying type, hinting to the user of functions or variables a certain meaning or format is expected. For example, in the code below, the type int is being further clarified to the distinct aliases Points and KmPerHour:

The use of typedef here is used to indicate that only variables declared as Points (or variables that can be interpreted as a Points type) should be used as the first argument, and variables declared as KmPerHour for the second. The value of using typedef in this manner would be for specific cases where masking the underlying type with an alias would add substantial clarity for the programmer, and not for any sort of type enforcement from the compiler, since typedefs are translated back to underlying types before any error checking occurs. For example, this code will compile, without any error:

Declaration simplification A typedef may be used to simplify the declarations of objects having types with verbose names, such as struct, union, or enum types. The point of this is to make it more convenient to declare variables with certain types. For example,

Along with providing previously declared types directly after the typedef keyword, one may also put a full definition of a struct, union, or enum. This has the effect of providing both a type definition and an alias in one statement.

When a definition is provided in the typedef declaration, the tag (i.e. name of the underlying type) is optional, leaving only the alias. Modifying the previous snippet:

In C++, unlike in C, the tag of a class, struct, union, or enum type is called a class name, and can be used as a type alias automatically without any typedef declaration.

The correspondence between this C++ feature and typedef is very strong, extending to the fact that it is possible to shadow the simple type name in a nested scope by declaring it as the identifier of another kind of entity. In such a case, the C-style full type name (an "elaborated type specifier") can still be used to refer to the class or enum type. In C++, then, MyStruct can be used anywhere that MyNewType can be used, as long as there are no other declarations of these identifiers. The reverse is not true, however, for C++ requires the class name for some purposes, such as the names of constructor methods. A notorious example where even C++ needs the struct keyword is the POSIX stat system call that uses a struct of the same name in its arguments:

Here both C as well as C++ need the struct keyword in the parameter definition.

Pointers The typedef may be used to define a new pointer type.

IntPtr is a new alias with the pointer type int*. The definition, IntPtr ptr;, defines a variable ptr with the type int*. So, ptr is a pointer which can point to a variable of type int. Using typedef to define a new pointer type may sometimes lead to confusion. For example:

Above, IntPtr cliff, allen; means defining 2 variables with int* type for both. This is because a type defined by typedef is a type, not an expansion. In other words, IntPtr, which is the int* type, decorates both cliff and allen. For IntPtr cliff2, *allen2;, the IntPtr type decorates the cliff2 and *allen2. So, IntPtr cliff2, *allen2; is equivalent to 2 separate definitions, IntPtr cliff2; and IntPtr* allen2. IntPtr* allen2 means that allen2 is a pointer pointing to a memory with int* type. Shortly, allen2 has the type, int**.

Constant pointers Again, because typedef defines a type, not an expansion, declarations that use the const qualifier can yield unexpected or unintuitive results. The following example declares a constant pointer to an integer type, not a pointer to a constant integer:

Since it is a constant pointer, it must be initialized in the declaration.

Structures and structure pointers Typedefs can also simplify definitions or declarations for structure pointer types. Consider this:

Using typedef, the above code can be rewritten like this:

In C, one can declare multiple variables of the same type in a single statement, even mixing structure with pointer or non-pointers. However, one would need to prefix an asterisk to each variable to designate it as a pointer. In the following, a programmer might assume that errptr was indeed a Node*, but a typographical error means that errptr is a Node. This can lead to subtle syntax errors.

By defining the typedef Node*, it is assured that all variables are structure pointer types, or say, that each variable is a pointer type pointing to a structure type.

Function pointers

The preceding code may be rewritten with typedef specifications:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Typedef

Start with the simplest possible case. Write down what Typedef claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Typedef before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Typedef ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Typedef

In research
Typedef appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Typedef in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Typedef is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, C (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Typedef outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Typedef” →

How to study Typedef in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Typedef means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Typedef out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Typedef in simple terms?

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred…

Why does Typedef matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Typedef?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Typedef.

Tags

  • C++
  • C (programming language)

Keep exploring