https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c

As Greg Hewgill said, the typedef means you no longer have to write struct all over the place. That not only saves keystrokes, it also can make the code cleaner since it provides a smidgen more abstraction.

Stuff like

typedef struct {
int x, y;
} Point; Point point_new(int x, int y)
{
Point a;
a.x = x;
a.y = y;
return a;
}

becomes cleaner when you don't need to see the "struct" keyword all over the place, it looks more as if there really is a type called "Point" in your language. Which, after the typedef, is the case I guess.

Also note that while your example (and mine) omitted naming the struct itself, actually naming it is also useful for when you want to provide an opaque type. Then you'd have code like this in the header, for instance:

typedef struct Point Point;

Point * point_new(int x, int y);

and then provide the struct definition in the implementation file:

struct Point
{
int x, y;
}; Point * point_new(int x, int y)
{
Point *p;
if((p = malloc(sizeof *p)) != NULL)
{
p->x = x;
p->y = y;
}
return p;
}

In this latter case, you cannot return the Point by value, since its definition is hidden from users of the header file. This is a technique used widely in GTK+, for instance.

UPDATE Note that there are also highly-regarded C projects where this use of typedef to hide struct is considered a bad idea, the Linux kernel is probably the most well-known such project. See Chapter 5 of The Linux Kernel CodingStyle document for Linus' angry words. :) My point is that the "should" in the question is perhaps not set in stone, after all.

Why should we typedef a struct so often in C? - Stack Overflow的更多相关文章

  1. 关于typedef在struct使用上的一些问题

    typedef struct lnode{ int data; struct lnode next; }lnode,linklist; 第一行的lnode是结构体名,最后一行的lnode是由typed ...

  2. typedef struct与struct的区别

    typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据 ...

  3. 关于typedef和struct

    在struct中使用自身,需要加struct关键字,无论带不带typedef,例如: struct A { int a; struct A *pA; }; 在定义struct方面尽量不要使用typed ...

  4. typedef & #defiine & struct

    #define(宏定义)只是简单的字符串代换(原地扩展),它本身并不在编译过程中进行,而是在这之前(预处理过程)就已经完成了. typedef是为了增加可读性而为标识符另起的新名称(仅仅只是个别名), ...

  5. 匿名字段 内嵌结构体 interface作为struct field 匿名接口

    interface作为struct field,谈谈golang结构体中的匿名接口 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/19 ...

  6. struct和typedef struct用法

    参考:http://www.cnblogs.com/qyaizs/articles/2039101.html C语言: typedef struct Student{ int score; }Stu; ...

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

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

  8. C++ typedef详解

    1.typedef的用途1)定义一种类型的别名注意typedef并不是简单的宏替换,如下例所示: int main() { char *pa,pb;//声明了一个指向字符变量的指针pa,和一个字符变量 ...

  9. 见怪不怪的typedef

    typedef是C++中的一个十分重要的关键字,它有强大的功能和方法的用途.但是有时候,碰到一些用到typedef的地方却感到很奇怪了. 给个栗子尝尝: typedef void(*pFun)(voi ...

随机推荐

  1. graphicsmagick常用命令

    显示图像文件详细信息 gm identify a.jpg 1.更改当前目录下.jpg的尺寸大小,并保存于目录.thumb里面 gm mogrify -output-directory .thumbs ...

  2. 【Python】paddlepaddle docker python2.7 centos运行

    PS E:\AI\BlockMining> docker cp .\LearnPaddle-master\ 30acd98dbc60:\rootPS E:\AI\BlockMining> ...

  3. PHP代码审计笔记--CSRF漏洞

    0x01 前言 CSRF(Cross-site request forgery)跨站请求伪造.攻击者盗用了你的身份,以你的名义向第三方网站发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻 ...

  4. Linux CentOS6.5上搭建环境遇到的问题

    1.卸载CentOS自带的JDK 查看centos上 安装的jdk:rpm -qa|grep jdk 出现如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86 ...

  5. C++ template —— 类型区分(十一)

    前面的博文介绍了模板的基础,深入模板特性,模板和设计的一些内容.从这篇开始,我们介绍一些高级模板设计,开发某些相对较小.并且互相独立的功能,而且对于这些简单功能而言,模板是最好的实现方法:(1)一个用 ...

  6. 十款不错的Hybrid App移动开发框架

    本文转载至http://www.pureasme.com/blog/2015/0419476.html ionic 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应 ...

  7. Android设置横屏竖屏

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FUL ...

  8. 《Windows内核编程》---系统线程和同步事件

    系统线程: 在驱动中生成的线程一般是系统线程,系统线程所在的进程名为“System”,用到的内核API函数是: NTSTATUS PsCreateSystemThread( OUT PHANDLE T ...

  9. C语言内存使用的常见问题及解决之道

    一  前言 本文所讨论的“内存”主要指(静态)数据区.堆区和栈区空间(详细的布局和描述参考<Linux虚拟地址空间布局>一文).数据区内存在程序编译时分配,该内存的生存期为程序的整个运行期 ...

  10. python基础---->python的使用(一)

    这里面记录一些python的一些基础知识,数据类型和变量.幸而下雨,雨在街上泼,却泼不进屋内.人靠在一块玻璃窗旁,便会觉得幸福.这个家还是像个家的. python的一些基础使用 一.python中的数 ...