Difference Between Struct and Typedef Struct in C++
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 and user-defined datatypes like class, struct, and union to give them more meaningful names. In this article, we will learn the differences between a struct and a typedef struct in C++.
Struct in C++
The struct keyword in C++ is used to define a structure. A structure is a user-defined composite data type that allows us to combine data of different data types together under a single name.
Syntax to Define a Struct
struct StructName {
data_type member_name1;
data_type member_name2;
};
Here,
- StructName is the name of the structure.
- member_name are the members (variables or functions) of the structure.
Example
The below example demonstrates the use of structure in C++.
// C++ program to use struct in C++
#include <iostream>
using namespace std;
// define a struct MyStruct
struct MyStruct {
int num;
void increase() { num += 5; }
};
int main()
{
// create object of MyStruct
struct MyStruct obj;
// access variable using dot operator
obj.num = 5;
// access member function using dot operator
obj.increase();
// printing the value of num
cout << "Number is: " << obj.num;
}
Output
Number is: 10
Typedef Struct in C++
The typedef struct in C++ is used to define a structure and create an alias for it. This allows us to use the alias instead of the struct keyword when defining variables of the structure type.
Syntax to Define a Typedef Struct
typedef struct {
// members
} AliasName;
Here,
- AliasName is the alias name for the structure.
- members are the variables or functions of the structure.
Example
The below example demonstrates the use of typedef struct in C++.
// C++ program to use typedef struct in C++
#include <iostream>
using namespace std;
// define a typedef struct MyStruct
typedef struct {
int num;
void increase() { num += 5; }
} MyStruct;
int main() {
// create object of MyStruct
MyStruct obj;
// access variable using dot operator
obj.num = 5;
// access member function using dot operator
obj.increase();
// printing the value of num
cout << "Number is: " << obj.num;
}
Output
Number is: 10
Difference Between Struct and Typedef Struct in C++
The below table demonstrates the key differences between struct and typedef struct in C++ are:
Feature | Struct | Typedef Struct |
---|---|---|
Definition | Used to define a structure with the struct keyword. | Used to define a structure and create an alias for it. |
Syntax | struct StructName { members }; | typedef struct { members } AliasName; |
Instantiation | Requires ‘struct’ keyword for declaration and instantiation. | Does not require ‘struct’ keyword when declaring objects of the type. |
Purpose | Groups together different data types into a single unit. | Simplifies the syntax for using the structure, reducing code verbosity. |
Usage of Aliases | Cannot directly create an alias for the type name | Allows creating an alias for the type name, improving code readability |
Additional Typedefs | Cannot create multiple typedefs for the same struct | Can create multiple typedefs for the same struct, providing flexibility in naming |
Conclusion
In conclusion, the choice between struct and typedef struct in C++ depends on whether we want to use the struct keyword every time we define a variable of the structure type. If we want to avoid this, we can use typedef struct to create an alias for the structure.
Contact Us