Open In App

Struct vs Typedef Struct in C++

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the struct keyword is used to define a struct, whereas the typedef keyword is used for creating an alias(new name) for existing datatypes or a user-defined datatypes like class, struct, and union to give them more meaningful names.

  • In C, programmers often write typedef struct { ... } Alias; to avoid repeating the keyword struct.
  • In C++, this is not required. The struct name itself is already a type alias.

struct in C++ 

  • The struct keyword defines a user-defined data type that groups variables under a single name.
  • In C++, once a struct is declared, its name can be used directly as a type without needing typedef.

Here is an example of struct.

C++
#include <iostream>
using namespace std;

// define a struct MyStruct
struct MyStruct
{
    int num;
    void increase()
    {
        num += 5;
    }
};

int main()
{
    MyStruct obj;
    obj.num = 5;
    obj.increase();
    cout << "Number is: " << obj.num << endl;

    return 0;
}

Output
Number is: 10

typedef struct in C++

Here is an example of typedef with struct. Please note that most of the time, we do not need typedef with struct. Here we have shown example to show that it is syntactically correct to use typedef.

C++
#include <iostream>
using namespace std;

typedef struct MyStruct
{
    int num;
    void increase()
    {
        num += 5;
    }
} MyStruct;

int main()
{
    MyStruct obj;
    obj.num = 5;
    obj.increase();
    cout << "Number is: " << obj.num << endl;

    return 0;
}

Output
Number is: 10

Difference Between Struct and Typedef Struct in C++

  • struct defines a user-defined composite type; typedef struct defines a struct and simultaneously creates an alias for it.
  • struct objects can be declared directly using the struct name; typedef struct objects can be declared using the alias without writing struct.
  • struct groups variables (and functions) under one type; typedef struct also provides a convenient alternative name for the type.
  • struct name itself acts as a type; typedef struct provides an explicit alias for the type.
  • struct is used directly; typedef struct can be replaced with using for clearer modern C++ type aliases.

Explore