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 ...
随机推荐
- RESTful api架构设计
阮老师的这两篇文章足够了 理解 RESTful 架构 RESTful API 设计指南
- vux 使用 loading 组件
1)声明引入Loading import { Loading } from 'vux' 2)在模版底部添加 组件(需要添加到 template>div 标签里) <template> ...
- CharacterMotor_刚体角色驱动
using UnityEngine; //this class holds movement functions for a rigidbody character such as player, e ...
- LINUX分辨率修改
上次说过了如何搭建LINUX虚拟机环境,但是完成之后存在很多问题,屏幕分辨太小就是其中之一. 为了让各位能有一个舒心的工作环境,现在就教给大家LINUX系统更改屏幕分辨率的两个办法. 一.鼠标操作 1 ...
- Xcode 插件集:xTextHandler
本文转载至 http://www.tuicool.com/articles/zIFvQn7 基于 Xcode Source Editor Extension 做了一个插件集,叫做 xTextHandl ...
- java 对 汉字排序(按照拼音字母排序)
业务场景: 一个list集合,里面add了若干个实体类,针对该实体类排序的属性为String. 使用技术,自定义list排序(JDK自带),重写Comparator接口的compare方法,汉字转拼音 ...
- PHP垃圾回收机制防止内存溢出
PHP语言同其他语言一样,具有垃圾回收机制.那么今天我们要为大家讲解的内容就是关于PHP垃圾回收机制的相关问题.希望对大家有所帮助. 一.PHP 垃圾回收机制(Garbage Collector 简称 ...
- [微信小程序]计算自己手机到指定位置的距离
目的: 根据目的地的坐标计算自己手机的位置离目的地的距离的 核心思路: 后续操作必须等所有异步请求都返回了才能继续 使用 const qqmap = require("../../utils ...
- iPhone 上如何通过 Safari 使用 Pocket
在开始之前,请确认你的机器上已经安装了 Pocket 应用软件. 如何安装 1.打开Pocket应用,点击左上角的菜单(三条横岗),找到最下面的 Help ,点击 How To Save ,找到 ...
- Android.mk (2) 函数进阶教程 - 分支、循环、子程序
https://www.jianshu.com/p/674dc7d7b4b0 函数进阶教程 - 分支.循环.子程序 按照面向过程程序设计的标准流程,我们讲完了顺序结构,就要讲分支.循环和子程序.下面我 ...