Structures in C++: How struct Differs from typedef struct — A Beginner's Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Do Beginners Get Confused Between struct and typedef struct?

\n\n

If you've ever looked at Stack Overflow searching for an answer to the question "Difference between 'struct' and 'typedef struct' in C++?", you've probably noticed that this topic has gathered thousands of views. And this is no coincidence. Many beginner C++ developers, especially those coming from the world of C or reading old textbooks, stumble upon the strange syntax with typedef struct and genuinely wonder: why write an extra word if you can just write struct?

\n\n

In this article, we will finally figure out the difference between struct and typedef struct in modern C++. We'll look at the historical roots of this confusion, write several code examples, and give clear recommendations on how to write correctly. Let's go!

\n\n

What is struct in C++?

\n\n

Let's start with the basics. A structure (struct) in C++ is a user-defined data type that groups several variables (fields) under one name. Unlike a class, all members of a structure have public access by default, but otherwise struct and class in C++ are practically identical (yes, a structure can have methods, constructors, and even inheritance).

\n\n

Declaring a structure in modern C++ looks like this:

\n\n
// Simple structure\nstruct Point {\n    int x;\n    int y;\n};\n\nint main() {\n    Point p1;          // OK: creating an object\n    p1.x = 10;\n    p1.y = 20;\n    return 0;\n}
\n\n

Simple and elegant. But where does typedef struct come from then?

\n\n

Historical Background: The Legacy of the C Language

\n\n

The thing is, in the C language, to create a variable of your structure type, you had to write the keyword struct before the structure name. For example:

\n\n
// In C, like this:\nstruct Point p1;  // without the word struct — error
\n\n

To get rid of this inconvenience, C programmers came up with a trick using typedef:

\n\n
// C-style: typedef + struct\ntypedef struct {\n    int x;\n    int y;\n} Point;\n\nint main() {\n    Point p1;  // now you can use it without struct\n    return 0;\n}
\n\n

This syntax creates an alias for an unnamed structure. In C, this was very convenient. But in C++, everything works differently.

\n\n

Is typedef struct Needed in C++?

\n\n

The short answer: no, it is not needed. In modern C++ (starting from the C++98 standard and onwards), the structure name automatically becomes a type name. You can use it without the struct keyword.

\n\n

Compare the two approaches:

\n\n
// Approach 1: Pure C++ (recommended)\nstruct Point {\n    int x, y;\n};\n\nPoint p1;  // OK\n\n// Approach 2: Outdated C-style (not recommended)\ntypedef struct {\n    int x, y;\n} Point;\n\nPoint p2;  // also OK, but why the extra word?
\n\n

Both options will compile, but the first one is shorter, clearer, and conforms to modern coding standards. The second option is just a habit inherited from C.

\n\n

When Might typedef struct Still Be Useful?

\n\n

There are a few situations where typedef struct in C++ might be justified:

\n\n
    \n
  • Cross-platform C and C++ code. If you are writing a header file that must compile in both C and C++ (e.g., for a library), it's better to use typedef struct — this way the code will work everywhere.
  • \n
  • Creating an alias for a complex type. Sometimes typedef is used with templates or pointers to structures to simplify the syntax.
  • \n
  • Compatibility with legacy code. If you are maintaining an old project written in C-style, it's better not to break the existing style.
  • \n
\n\n

But if you are writing a new project in pure C++, forget about typedef struct

Blogs

Book Recommendations