原文:http://www.nowamagic.net/librarys/veda/detail/1785

typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。

在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。

至于typedef有什么微妙之处,请接着看下面对几个问题的具体阐述。

首先在C中定义一个结构体类型要用typedef:

typedef struct Student
{
int a;
}Stu;

于是在声明变量的时候就可:Stu stu1;

如果没有typedef就必须用struct Student stu1;来声明。

这里的Stu实际上就是struct Student的别名。

另外这里也可以不写Student(于是也不能struct Student stu1;了)。

typedef struct
{
int a;
}Stu;

但在c++里很简单,直接

struct Student
{
int a;
};

于是就定义了结构体类型Student,声明变量时直接Student stu2。

其次在C++中如果用typedef的话,又会造成区别:

struct Student
{
int a;
}stu1; //stu1是一个变量 typedef struct Student2
{
int a;
}stu2; //stu2是一个结构体类型

使用时可以直接访问stu1.a,但是stu2则必须先 stu2 s2;,然后 s2.a=10;

掌握上面两条就可以了,不过最后我们探讨个没多大关系的问题。

如果在c程序中我们写:

typedef struct
{
int num;
int age;
}aaa,bbb,ccc;

这算什么呢?

我个人观察编译器(VC6)的理解,这相当于

typedef struct
{
int num;
int age;
}aaa;
typedef aaa bbb;
typedef aaa ccc;

也就是说aaa,bbb,ccc三者都是结构体类型。声明变量时用任何一个都可以,在c++中也是如此。但是你要注意的是这个在c++中如果写掉了typedef关键字,那么aaa,bbb,ccc将是截然不同的三个对象。

C语言中的struct和typedef struct<转载>的更多相关文章

  1. [转载]彻底弄清struct和typedef struct

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  2. struct和typedef struct彻底明白了

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  3. struct和typedef struct

    转自:http://www.cnblogs.com/qyaizs/articles/2039101.html struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++ ...

  4. struct和typedef struct的用法

    我首先想到的去MSDN上看看sturct到底是什么东西,虽然平时都在用,但是每次用的时候都搞不清楚到底这两个东西有什么区别,既然微软有MSDN,我们为什么不好好利用呢,下面是摘自MSDN中的一段话: ...

  5. 细说 struct和typedef struct

    细说 struct和typedef struct 参考原文:http://www.cnblogs.com/qyaizs/articles/2039101.html,有些小改动~ 1 首先://注意在C ...

  6. C++/C中的struct和typedef struct用法和区别

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  7. struct和typedef struct在c++中的用法

    #include<iostream> using namespace std; struct test{ int a; }test; //定义了结构体类型test,声明变量时候直接test ...

  8. struct和typedef struct在c语言中的用法

    在c语言中,定义一个结构体要用typedef ,例如下面的示例代码,Stack sq:中的Stack就是struct Stack的别名. 如果没有用到typedef,例如定义 struct test1 ...

  9. struct和typedef struct的区别

    当typedef与结构结合使用时,会有一些比较复杂的情况,而且在C语言和C++里面有略有差别,因此从网上摘录了一些资料. 1 首先:      在C中定义一个结构体类型要用typedef:       ...

随机推荐

  1. ubuntu apt-get修改源地址

    亲测搜狐可用,其他备用 1.修改源地址:cp /etc/apt/sources.list /etc/apt/sources.list.bakvim /etc/apt/sources.list 修改之后 ...

  2. 一个优秀的Android应用从建项目开始

    1.项目结构 现在的MVP模式越来越流行.就默认采用了.如果项目比较小的话: app——Application Activity Fragment Presenter等的顶级父类 config——AP ...

  3. EntityFramework系列:Repository模式与单元测试

    1.依赖IRepository接口而不是直接使用EntityFramework 使用IRepository不只是架构上解耦的需要,更重要的意义在于Service的单元测试,Repository模式本身 ...

  4. [C/C++基础] C语言常用函数memset的使用方法

    函数声明:void *memset(void *s, int ch, size_t n); 用途:为一段内存的每一个字节都赋予ch所代表的值,该值采用ASCII编码. 所属函数库:<memory ...

  5. 第五章 与众不同的this

    我们来看下面的代码: ①var name="windows"; function myfun() //定义一个函数myfun { console.log("I'm &qu ...

  6. gulp初体验记录(简介、插件开发介绍)

    目前用的业界比较知名的三个前端构建工具:grunt.gulp.fis,自己此前一直都是只在用grunt,fis看过一点,gulp则一直都没注意过,直到最近发现好像用的人越来越多,所以今天也就抽了点时间 ...

  7. C# txt格式记录时间,时间对比,决定是否更新代码记录Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. 传智168期JavaEE就业班 day04-dom

    * 课程回顾: * js语法 * js的动态函数和匿名函数 * js动态函数 Function new Function(); * 匿名函数:没有名称的函数,起个名称 var add = functi ...

  9. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)

    题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...

  10. Android中获取图片的宽和高

    在Android中,我们想获取图片的宽和高应该怎么办?一.正常加载图片的方法下获取宽和高 举一个简单的例子:创建一个图片的副本 //加载原图 Bitmap bmSrc = BitmapFactory. ...