关于typedef的一些小知识

//关于typedef

//1.在c语言中定义一个结构体
typedef struct student{
int a;
}stu;
//typedef 给结构体起了个别名 stu;
//于是,在声明变量的时候就可以用: stu stu1;
//如果没有typedef语句,就必须使用 struct student stu1;(c语言,不包括c++)
//在c++中可以直接 student stu1;也可以struct student stu1;都是创建了结构体变量 stu1;

//2.
typedef struct student{
int a;
string s="hello";
}stu,aa,bb,cc;

//给这个结构体起了多了别名,分别为stu,aa,bb,cc;
//声明时任用其一就可以;

//3.去掉typrdef
struct student{
int a;
string s="hello";
}stu;//此时stu是一个结构体变量
//可以用stu.a来访问内部元素;

#include<iostream>
using namespace std;
typedef struct student{
    int a;
    string s="hello";
}stu,aa,bb,cc;
int main()
{
    aa a1;//以下5句都是声明语句 
    bb b1;
    stu stu2;
    student stu1;
    struct student stu3;
    
    cout<<a1.s<<endl;
    cout<<b1.s<<'
'<<stu2.s<<'
'<<stu3.s<<endl; 
    return 0;
 } 

原文地址:https://www.cnblogs.com/qinmin/p/12234987.html