Struct and typedef struct

Source: Internet
Author: User


1 first: // note the differences between C and C ++
To define a struct type in C, use typedef:
Typedef struct student
{
Int;
} Stu;
So when declaring the variable, you can: Stu stu1;
The Stu here is actually the alias of struct student. Stu = struct student
  
However, in C ++
Struct student
{
Int;
};
As a result, student of the struct type is defined, and student stu2 is directly used when the variable is declared;
========================================================== ========================================================== ======


2. Second:
In C ++, if typedef is used, the following differences may occur:
Struct student
{
Int;
} Stu1; // stu1 is a variable.

 
Typedef struct student2
{
Int;
} Stu2; // stu2 is a struct type = struct student

 
You can directly access stu1.a during use.
But stu2 must first stu2 S2;
Then s2.a = 10;
========================================================== ========================================================== ====


Summary:

C/C ++ can all be like this:

Typedef struct student
{
Int;
} Stu;


In this case, Stu is a type and Stu stu1 can be used to declare variables;

However, C ++ is simple:

Struct student
{
Int;
};

At this time, student is a type with the declared variable: Student stu1


Remember to sum up.

Struct and typedef struct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.