Structure declaration C / C ++ in: struct and typedef struct usage

The conventional definition and create the structure of

TYPEDEF no way of declaration

First, define the general structure, you must have been very clear:

struct Person{
    int age;
    string name;
};

Here Person is the name of the structure of your statement, that one type, such as int.

If you want to declare several variables of Person, then there are two ways:

// 1. 在声明之后立刻创建
struct Person {
    int age;
    string name;
    } person_a, person_b;
// 这里 person_a 都是实际的 variables 啦!

// 2. 随后需要的时候再创建,给出C中的定义方法
struct Person person_c;

There TYPEDEF declaratively

In C, the structure must create a struct in front of Cadogan, a bit cumbersome. Of course, there is a simple way is to add a typedef.

typedef struct Person {
    int i;
    string name;
} Ren;
// 这里 Ren 不是 variable, 而是一个类型名
// 通过这种方式声明的结构体就可以较为简洁地创建变量
Ren person_d;

typedef did not actually participate in the definition of the structure, but only to the structure you create a nickname. In this example, struct Person's nickname is Ren

 

Reference https://www.cnblogs.com/ericwannn/p/10548186.html

Published 30 original articles · won praise 0 · Views 258

Guess you like

Origin blog.csdn.net/andy1515/article/details/104036612