Why should we typedef a struct so often in C? - Stack Overflow
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的更多相关文章
- 关于typedef在struct使用上的一些问题
typedef struct lnode{ int data; struct lnode next; }lnode,linklist; 第一行的lnode是结构体名,最后一行的lnode是由typed ...
- typedef struct与struct的区别
typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据 ...
- 关于typedef和struct
在struct中使用自身,需要加struct关键字,无论带不带typedef,例如: struct A { int a; struct A *pA; }; 在定义struct方面尽量不要使用typed ...
- typedef & #defiine & struct
#define(宏定义)只是简单的字符串代换(原地扩展),它本身并不在编译过程中进行,而是在这之前(预处理过程)就已经完成了. typedef是为了增加可读性而为标识符另起的新名称(仅仅只是个别名), ...
- 匿名字段 内嵌结构体 interface作为struct field 匿名接口
interface作为struct field,谈谈golang结构体中的匿名接口 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/19 ...
- struct和typedef struct用法
参考:http://www.cnblogs.com/qyaizs/articles/2039101.html C语言: typedef struct Student{ int score; }Stu; ...
- struct和typedef struct在c++中的用法
#include<iostream> using namespace std; struct test{ int a; }test; //定义了结构体类型test,声明变量时候直接test ...
- C++ typedef详解
1.typedef的用途1)定义一种类型的别名注意typedef并不是简单的宏替换,如下例所示: int main() { char *pa,pb;//声明了一个指向字符变量的指针pa,和一个字符变量 ...
- 见怪不怪的typedef
typedef是C++中的一个十分重要的关键字,它有强大的功能和方法的用途.但是有时候,碰到一些用到typedef的地方却感到很奇怪了. 给个栗子尝尝: typedef void(*pFun)(voi ...
随机推荐
- junit的简单用法
之前测试一个方法总要写一个main函数来调用,感觉既费事又有点low.今天来简单研究一下怎么使用junit来进行单元测试. 1. 依赖包 <dependency> <groupId& ...
- java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available
好久没有使用MyEclipse10了,今天打开看了以前大学的项目,在Tomcat7中发布启动,我嚓嘞,报错: SEVERE: Exception initializing random number ...
- python对oracle数据库的操作
1 Oracle数据库 1.1 Oracle环境配置&客户端连接 1.1.1 下载安装Oracle绿色版客户端instantclient: 到o ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- “std”: 具有该名称的命名空间不存在
当只用using namesp std 时,会报 error C2871: “std”: 具有该名称的命名空间不存在. 包含一个含有std的头文件就不会报错了,比如<iostream>.& ...
- 【PHP】 毫秒级时间戳和日期格式转换
在并发量搞得情况下.需要开启毫秒级运算 mysql 支持: `create_time` datetime() DEFAULT NULL COMMENT '创建时间', 效果 PHP 代码实现: &l ...
- liunx trac 邮件提示功能
http://trac.edgewall.org/wiki/TracNotification官网上提供的方法.个人觉得不是清楚,不过还是有参考价值的.以下写下自己的添加过程,以作记录. 1.the [ ...
- C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...
- Promise {<pending>
场景:在create-react-app whatwg-fetch的项目中,想获取请求返回的数据, componentWillMount() { console.log(this.props) con ...
- POJ 1117 Pairs of Integers
Pairs of Integers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 Accepted: 1062 Des ...