Struct definition typedef struct usage explanation and usage Summary

Source: Internet
Author: User

Typedef indicates the type definition. Typedef struct is convenient to use this struct.
The specific difference is:
If struct node {} is used to define the struct. When applying for a node variable, you need to write it like this, struct node N;
If typedef is used, you can write typedef struct node {} node ;. When applying for a variable, you can write it like this, node N;
The difference is whether the keyword struct can be omitted during use.

 

Article 3: struct and typedef struct

It can be divided into three parts:
1 first:
To define a struct type in C, use typedef:
Typedef struct student
{
Int;
} Stu;
So when declaring the variable, you can: Stu stu1;
If no typedef exists, you must use struct student stu1; to declare
The Stu here is actually the alias of struct student.
In addition, student can also be left empty here (so it cannot be struct student stu1)
Typedef struct
{
Int;
} Stu;
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.
You can directly access stu1.a during use.
But stu2 must first stu2 S2;
Then s2.a = 10;
========================================================== ===
3. You can master the above two items, but at last we will discuss the issues that do not matter much.
If we write:
Typedef struct
{
Int num;
Int age;
} AAA, BBB, CCC;
What is this?
I personally observe the understanding of the compiler (vc6), which is equivalent
Typedef struct
{
Int num;
Int age;
} AAA;
Typedef aaa bbb;
Typedef aaa ccc;
That is to say, AAA, BBB, and CCC are all struct types. When declaring a variable, you can use any one. This is also true in C ++. However, note that if the typedef keyword is written in C ++, AAA, BBB, and CCC will be completely different objects.

Article 4: usage of typedef struct and struct in C/C ++

Struct _ X1 {...} x1; and typedef struct _ X2 {...} X2; what is the difference?

In fact, the former defines the object instance X1 of class _ X1 and _ X1, and the latter defines the class name X2 of class _ X2 and _ X2,

Therefore, they take other information during use. Please refer to instance 1.

[Knowledge point]

Structure is also a type of data. You can use Structure Variables. Therefore, like other types of variables, you must first define them when using structure variables.

The general format for defining structure variables is:

Struct structure name

{

Type variable name;

Type variable name;

...

} Structure variable;

The structure name is the structure identifier, not the variable name.

Another common format is:

Typedef struct structure name

{

Type variable name;

Type variable name;

...

} Structure alias;

Note: In C, struct cannot contain functions. In C ++, struct is expanded to include functions.

========================================================== ====================================

Instance 1: struct. cpp

# Include <iostream>

Using namespace STD;

Typedef struct _ point {

Int X;

Int y;

} Point; // defines a class, giving the class an alias

Struct _ Hello {

Int X, Y;

} Hello; // define classes and objects at the same time

Int main ()

{

Point pt1;

Pt1.x = 2;

Pt1.y = 5;

Cout <"ptpt1.x =" <pt1.x <"PT. Y =" <pt1.y <Endl;

// Hello pt2;

// Pt2.x = 8;

// Pt2.y = 10;

// Cout <"pt2pt2. x =" <pt2.x <"pt2.y =" <pt2.y <Endl;

// The above Hello pt2; this line of compilation will not pass. Why?

// Because hello is the defined object instance.

// The correct method is as follows: Use hello. X and hello. Y

Hello. x = 8;

Hello. Y = 10;

Cout <"hellohello. x ="

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.