Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Difference Between Struct and Typedef Struct in C++



In C++ program, struct is a keyword used to define a structure, while typedef is a keyword that is used to create an alias for a data type. In this article, we will discuss the difference between struct and typedef struct in C++.

Struct in C++

Struct is a keyword used to define a structure in C++. A structure is a user-defined data type that groups related variables of different data types into a single unit. Struct is same as a class, but the members of a struct are public by default. The syntax for defining a struct is as follows:

struct StructureName {
    dataType1 member1;
    dataType2 member2;
    // ...
};

Here, structureName is the name of the structure, and member1, member2, etc. are the members of the structure. These members can be of different data types. Now let's see an example of using struct in C++.

Example

In the code below, we have defined a struct called Person that contains two members: name and age. We then created an object of the struct and accessed its members.

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
};

int main() {
    Person person1;
    person1.name = "John";
    person1.age = 30;

    cout << "Name: " << person1.name << endl;
    cout << "Age: " << person1.age << endl;

    return 0;
}

The output of the above code will be:

Name: John
Age: 30

Typedef Struct in C++

The typedef struct keyword is used to create an struct and give it a new name. This is modern way of defining a struct which supports in both C and C++ syntax. For example, you can define a struct and give it a new name using typedef as follows:

typedef struct Rectangle {
    int width;
    int height;
} Rect;

Here, we have defined a struct named Rectangle and given it an alias name Rect. Now, we can use Rect as a data type to create objects of the struct. For the above struct, we can create objects in following ways:

struct Rectangle r1;  // Using the struct name
Rect r2;              // Using the typedef alias

You can see that, the second way is more simple and easy to read. This is the main reason why typedef struct is used in C and C++.

Example

In the code below, we have defined a struct called Coordinate. By using typedef we have given it an alias name Point to it.

#include <iostream>
using namespace std;

typedef struct Coordinate {
    int x;
    int y;
} Point;

int main() {
    Point point1;
    point1.x = 10;
    point1.y = 20;

    cout << "X: " << point1.x << endl;
    cout << "Y: " << point1.y << endl;

    return 0;
}

The output of the above code will be:

X: 10
Y: 20

Difference between Struct and Typedef Struct

Here are the key differences between struct and typedef struct:

Struct Typedef Struct
Struct is a keyword used to create a struct. Typedef struct is used to create a struct and define an alias for it.
Struct keyword can be used in both C and C++. In C++, typedef struct is not necessary, but it is used for compatibility with C.
If you define a struct with struct keyword, you need to use the struct keyword every time you create an object of the struct (in case of C). If you define a struct with typedef, you can use the alias name directly to create an object of the struct.
Example:
struct Box {
    int length;
    int width;
};
struct Box b1;
Example:
typedef struct Box {
    int length;
    int width;
} BoxAlias;
BoxAlias b2;
Updated on: 2025-05-26T18:20:27+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements