typedef struct 详解

本文详细介绍了在C和C++中如何定义结构体类型,包括使用typedef简化类型的定义方式,以及直接定义结构体变量的方法。对于C++,还介绍了在定义结构体时声明变量的语法。

一、在C中定义一个结构体类型要用typedef
1、
typedef struct tagStudent
{
int a;

}Stu;

Stu实际上就是struct tagStudent的别名,声明变量可以 struct tagStudent stu1; 或者Stu stu2;

2、
typedef struct
    {
    int a;
    }Stu;
声明变量只能是Stu stu1;

二、在C++中可以简单地
1、
struct Student
    {
    int a;
    };
Student 是结构体类型,可以直接声明变量,Student stu1;

2、
struct Student
    {
    int a;
    }stu2;//stu1是一个变量

Student 是结构体类型,可以直接声明变量,Student stu1; stu2已经是一个变量;

3、typedef struct tagStudent
{
int a;

}Stu,*pStu;

pStu 与typedef struct tagStudent* pStudent 中的pStudent是等价的。类似 typedef int* p;
表示定义一种数据类型,该类型是指向结构体的,pStu pstu1声明了pstu1是一个指向结构体的指针;使用如下:
pStu pstu1;
pstu1 = malloc(sizeof(Stu));
pstu1->a=20;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值