Structure definition typedef struct Usage Brief and usage summary

Source: Internet
Author: User

A typedef is the meaning of a type definition. typedef structs are designed to facilitate the use of this structure. The specific difference is that if struct node {} is defined as a struct. This is required when applying the variable for node, struct node n; If using TypeDef, can write this, typedef struct NODE{}NODE; This can be written when applying variables, NODE n; The difference is whether you can omit the struct keyword when you use it.

Article III: struct and typedef structs

three blocks to tell: 1 First: Define a struct type in C to use the typedef:typedef struct Student {int A;} Stu; So when declaring variables, you can: Stu stu1; If there is no TypeDef, you must use struct Student stu1 to declare that the STU here is actually the alias of the struct Student. In addition here can also not write Student (so also cannot struct Student stu1;) typedef struct {int A;} Stu; But in C + + very simple, direct struct Student {int A;}; Therefore, the structure type student is defined, and the variable is declared directly student stu2; =========================================== 2 Second: in C + +, if you use TypeDef, it will make a difference: struct Student {int A;} STU1;//STU1 is a variable typedef struct STUDENT2 {int A;} STU2;//STU2 is a struct type that can be accessed directly when used stu1.a but STU2 must first STU2 S2; Then s2.a=10; =========================================== 3 Master the above two, but in the end we will discuss a matter of little concern if we write in the C program: typedef struct {int num; int age ; }AAA,BBB,CCC; What the hell is this? My personal observation compiler (VC6) understands that this is equivalent to a typedef struct {int num; int age;} AAA, typedef AAA BBB; typedef AAA CCC; That is to say, AAA,BBB,CCC are all structural types. Any one can be used to declare a variable, as is the case in C + +. But what you should note is that in C + + if the typedef keyword is written out, then AAA,BBB,CCC will be a distinct three object.

Fourth: The use of typedef structs and structs in C + +

struct _X1 {...} X1; and typedef struct _x2{...} x2; What's the difference?

In fact, the former is an object instance x1 that defines classes _x1 and _x1, which is the class name _x2 that defines classes _x2 and x2,

So they are used in the process of taking something else. See Example 1.

[Knowledge Point]

Structs are also a type of data that can be used with structural variables, so, like other types of variables, they are defined first when using structural variables.

The general format for defining structure variables is:

struct structure name

{

Type variable name;

Type variable name;

...

} structure variables;

The struct name is a struct identifier and is not a variable name.

Another common format is:

typedef struct struct Name

{

Type variable name;

Type variable name;

...

} structure alias;

Also note: In C, a struct cannot contain a function. In C + +, structs are extended and can contain functions.

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

Example 1:struct.cpp

#include <iostream>

using namespace Std;

typedef struct _point{

int x;

int y;

}point; Define class, give 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 an object instance that has been defined.

The correct practice is as follows: with Hello.x and HELLO.Y

hello.x = 8;

HELLO.Y = 10;

cout<< "hellohello.x=" << hello.x << "hello.y=" <

return 0;

}

Fifth: Quiz

Q: What is the difference between defining a struct with a struct and a typedef struct? Why are there two ways of doing it?

struct Student {int A;} stu; typedef struct STUDENT2 {int A;} STU2;

A:

In fact, this thing is left over from the C language, typedef can define a new compound type or an alias for an existing type, in C, if you use struct XXX {}; method, you must use the struct XXX var to declare the variable, and the use of typedef struct {} method can be written as xxx var; However, there is no such thing in C + +, no matter which one you use, you can declare the variable in the second way, this should be the dregs of C language.

Summary of Usage

First to fourth use

Use one:

Defines an alias for a type, not just a simple macro substitution. Can be used as multiple objects that declare a pointer type at the same time. For example: char* PA, PB; The majority does not conform to our intention, it only declares a pointer to a character variable,//and a character variable; The following is possible: typedef char* PCHAR; General use capital PCHAR PA, PB; Possible, while declaring two pointers to character variables although: char *pa, *PB; Also feasible, but relatively not in the form of TypeDef intuitive, especially in the need of a large number of pointers, the way typedef is more convenient.

Use two:

Used in the old C code (specific How old is not checked), to help the struct. In the previous code, when declaring a struct new object, it was necessary to bring a struct with the form: struct struct name Object name, such as: struct tagPOINT1 {int x; int y;}; struct tagPOINT1 p1;

In C + +, you can write directly: Structure name Object name, namely: TagPOINT1 P1;

It is too troublesome to think that someone often writes more than one struct, so it is invented: typedef struct TAGPOINT {int x; int y;} Point;

Point P1; This is less than the original way to write a struct, more convenient, especially in the large use of time

Perhaps, in C + +, this use of TypeDef is not very large, but understanding of it, to master the old code is still helpful, after all, we may encounter in the project earlier legacy code.

Structure definition typedef struct Usage Brief and usage summary

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.