c/c++ typedef

C/C++中结构体的定义和typedef的使用 最近复习数据结构的时候,总是分不清struct、typedef struct和他们后面跟的tag名称的位置和含义,今天就来翻书记录下具体结构体的定义和typedef的使用。 一、结构体定义 1.C语言中结构体定义:C允许用户建立由不同类型的数据组成的组合型的数据结构。 struct 结构体名  {成员表列};  只是建立了一个结构体类型,相当于模型而没有定义变量,其中并无具体数据。因此,需要 阅读详情
 
Home	

C++ Keywords: typedef
 

An Alias for a Known Type

In C++, you can declare a variable using one of the built-indata types:

#include <iostream>

using namespace std;



int main()

{

	int number = 248;

	return 0;

}

In the same way, you can declare an array or a pointer. Youcan also declare various variables on the same line. Here are examples:

#include <iostream>

#include <string>

using namespace std;



int main()

{

	int number = 248;

	double *distance = new double(390.82);

	string countries[] = { "Ghana", "Angola", "Togo",

	                       "Tunisia", "Cote d'Ivoire" };



	cout << "Number: " << number << endl;

	cout << "Distance: " << *distance << endl;

	cout << "Countries:\n";

	for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

		cout << '\t' << countries[i] << endl;



	cout << endl;

	return 0;

}

If you plan to use the same type of data for manydeclarations, you can customize its name. In C++, thetypedef keywordallows you to create an alias for a data type. The formula to follow is:

typedef [attributes] DataType AliasName;

The typedef keyword is required. The attributeis not.

The typedef keyword can be followed by any C++ built-in datatype, includingint, short, signed, unsigned,char,signed char, unsigned char,double, long, or longdouble. The data type can also be an existing class that either is providedby one of the libraries that ship with the C++ compiler. For example, it can bethestring class. The data type can also be a pointer to a known type.

On the right side of the data type, type the name that willbe used to represent the data type or the pointer. Here are examples:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];



int main()

{



	cout << endl;

	return 0;

}

    In typedef short SmallNumber, SmallNumber can now be used as a data type to declare a variable for a small integer.
    In the second example, typedef unsigned int Positive, The Positive word can then be used to declare a variable of anunsigned int type.
    After typedef double* PDouble, the word PDouble can be used to declare a pointer to double.
    By defining typedef string FiveStrings[5], FiveStrings can be used to declare an array of 5 strings with each string being of typestring.

Based on this rule, you can declare the variables based on types you have typed defined. Here are examples:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];



int main()

{

	SmallNumber temperature = -248;

	Positive height = 1048;

	PDouble distance = new double(390.82);

	FiveStrings countries = { "Ghana", "Angola", "Togo",

		                  "Tunisia", "Cote d'Ivoire" };



	cout << "Temperature: " << temperature << endl;

	cout << "Height:      " << height << endl;

	cout << "Distance:    " << *distance << endl;

	cout << "Countries:\n";

	for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

		cout << '\t' << countries[i] << endl;



	cout << endl;

	return 0;

}

This would produce:

Temperature: -248

Height:      1048

Distance:    390.82

Countries:

        Ghana

        Angola

        Togo

        Tunisia

        Cote d'Ivoire

Type-Defining a Function

The typedef keyword can also be used to define an alias for a function. In this case, you must specify the return type of the function and its type(s) of argument(s), if any. Another rule is that the definition must specify that it is a function pointer. Here is an example:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];

typedef double (*Addition)(double value1, double value2);



double Add(double x, double y)

{

	double result = x + y;

	return result;

}



int main()

{

	SmallNumber temperature = -248;

	Positive height = 1048;

	PDouble distance = new double(390.82);

	FiveStrings countries = { "Ghana", "Angola", "Togo",

		                      "Tunisia", "Cote d'Ivoire" };

	Addition plus;



	cout << "Temperature: " << temperature << endl;

	cout << "Height:      " << height << endl;

	cout << "Distance:    " << *distance << endl;

	cout << "Countries:\n";

	for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

		cout << '\t' << countries[i] << endl;



	plus = Add;

	cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl;

	return 0;

}

This would produce:

Temperature: -248

Height:      1048

Distance:    390.82

Countries:

        Ghana

        Angola

        Togo

        Tunisia

        Cote d'Ivoire

3855.06 + 74.88 = 3929.94

The Attribute of a Type-Definition

The typedef keyword can be followed by an attribute before the data type. In its simplest form, the attribute can be that of an access level such aspublic, private, or protected. Here are examples:

#include <iostream>

#include <string>

using namespace std;



typedef public short SmallNumber;

typedef private unsigned int Positive;

typedef protected double* PDouble;

typedef public string FiveStrings[5];

typedef private double (*Addition)(double value1, double value2);



double Add(double x, double y)

{

	double result = x + y;

	return result;

}



int main()

{

	SmallNumber temperature = -248;

	Positive height = 1048;

	PDouble distance = new double(390.82);

	FiveStrings countries = { "Ghana", "Angola", "Togo",

		                  "Tunisia", "Cote d'Ivoire" };

	Addition plus;



	cout << "Temperature: " << temperature << endl;

	cout << "Height:      " << height << endl;

	cout << "Distance:    " << *distance << endl;

	cout << "Countries:\n";

	for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

		cout << '\t' << countries[i] << endl;



	plus = Add;

	cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl << endl;

	return 0;

}

In C++, the attribute of the typedef is usually applied as a class, a struct, or an enum. Here are examples:

#include <iostream>

#include <string>

using namespace std;



typedef struct Student;

typedef class Country;



typedef public short SmallNumber;

typedef private unsigned int Positive;

typedef protected double* PDouble;

typedef public string FiveStrings[5];

typedef private double (*Addition)(double value1, double value2);



struct Student

{

	string FirstName;

	string LastName;

};



typedef struct _Empl

{

	string FullName;

	double HourlySalary;

}Employee;



class Country

{

	string Name;

	string Capital;

	string Code;

};



double Add(double x, double y)

{

	double result = x + y;

	return result;

}



typedef enum EmplStatus { esFullTime, esPartTime, esContractor };

typedef Student *PStudent;

typedef Country *PCountry;



int main()

{

	Student pupil;

	Country pais;

	EmplStatus emplst;

	PStudent ptrStd = new Student;

	PCountry pPais = new Country;



	return 0;

}

 
	
 
Home	Copyright � 2005 FunctionX, Inc.	


vs code C/C++语法高亮配置文件(C/C++ Themes插件) vscode配色插件的c/c++语法高亮配置文件,主题插件为C/C++ Themes。 可以对诸如const、enum、typedef别名、结构体引用等语法高亮进行设置,语言本身的关键字自然不用说了,比one dark pro等热门的插件颜色丰富的多。 根据自己喜好对c常用语法颜色进了仔细设置,并加有注释。 立即下载

相关推荐

Colour_themes.conf

codeblocks官方颜色主题包代码

VSCode C/C++ Theme主题自定义配色方案

针对 C/C++ Theme 插件内的主题进行自定义选择性优化。

Telly_的博客 7549

关于STM32编译报错:Error: L6218E: Undefined symbol SystemInit (referred from startup_stm32f10x_md.o).

今天新建工程发现出现一个错误 —— …\Output\CG.axf: Error: L6218E: Undefined symbol SystemInit (referred from startup_stm32f10x_md.o). 但我觉得我的操作没有任何问题,而且配置和上一个文件比较是一模一样的。 于是我开始百度查询,然后发现了下面几个博客—— @weixin_44133391——STM32 编译错误 @dorlolok——iel5 编译报错 error: L6235E: More than one

纯粹,学术,热爱。 9401

通过更改vscode中C/C++ Themes插件中的.json文件自定义C/C++的编程主题

为什么我决定自定义主题?因为原来的暗色主题实在是太丑了!!!,但是其他的好看的暗色主题,比如 one dark pro(我个人认为)对于C和C++的语法显示方面又不够全面。所以我决定自定义一个主题来实现两者兼顾的想法(既要又要)既要层次分明,又要好看,最好还能随意更改(等我看腻了随时换)。先来展示一下我的修改后的主题好看的主题并不能提高生产力,但能提升使用者的心情,但是不要过度沉迷自定义主题,过度注重细节也会让心情变差,就像我写这篇博客时的心情一样,越写越烦躁,但又想写完。

2303_76820730的博客 2831

电机传动机构的转矩与惯量折算

参考《工业运动控制》

庐州牧 1万+

Oracle 11g版本下载及安装超详细教程图解

新手Oracle安装教程

weixin_43738344的博客 9万+

#vs code# 主题&C/C++开发插件

清凉简装 2019-11-27 12:10:30 11057 收藏 40 分类专栏: 生产力工具 文章标签: vscode 插件 版权 文章目录 1. C/C++ for Visual Studio Code 2. C++ Intellisense 3. Git Graph 4. compareit 5.TODO Highlight 6. Bookmarks 7. Markdown All in One 8. Markdown Preview Github Styling 9. Vscode Go.....

潇汀 4022

AudioTrack音频播放流程深度解析

深入解析Android AudioTrack音频播放的完整链路:从Java API到Native层,从共享内存Ring Buffer机制到FAST Track低延迟优化,配合源码分析带你彻底搞懂Android音频播放的底层原理

Cheson的专栏 938

Visual Studio Code 高效开发 C/C++ 插件推荐

Visual Studio Code 高效开发 C/C++ 插件推荐

世界那么大,我想去看看~【随心更新,看世界去了😄】 2万+

《数据结构》知识点汇总+算法代码总结【全】

写在前面:本文写于吴签时期,在家备考时刷完数据结构王道书之后想着把书中重点梳理汇总一下。本文内容包括但不局限于王道数据结构每章的知识点及其课后习题所涵盖的知识点。本人曾在大三期间打过一些程序设计类比赛,所以本文所涉及到的代码不一定局限于王道书,但思想都一样。 期末复习和备考408均可使用第一章:绪论(不在考研大纲但很重要)第二章:线性表第三章:栈和队列第四章:串第五章:树与二叉树第六章:图第七章:查找 第一章:绪论(不在考研大纲但很重要) 数据结构三要素:逻辑结构、存储结构、数据的运算;其中逻辑结构包括线

qq_44622401的博客 17万+

RK3588平台开发系列讲解(项目篇)常见模型结构

📢 AI 模型常采用人工神经网络来模拟人脑神经的记忆和处理信号的能力。常见的人工神经网络类型有深度神经网络(Deep Neural Network,DNN)、卷积神经网络(Convolutional Neural Network,CNN)、循环神经网络(Recurrent Neural Network,RNN)等。

内核笔记 1756

【C/C++】typedef用法详解

typedef 声明,简称 typedef,人们常常使用 typedef 来编写更美观和可读的代码。所谓美观,意指 typedef 能隐藏笨拙的语法构造以及平台相关的数据类型,从而增强可移植性和以及未来的可维护性。本文下面将竭尽全力来揭示 typedef 强大功能以及如何避免一些常见的陷阱。 一、typedef的四个用途 1.1、用途一:定义一种类型易于记忆的别名,而不只是简单的宏替换。 a...

qq_34018840的博客 5829

IGCT 测试原理概述

IGCT(Integrated Gate-Commutated Thyristor,集成门极换流晶闸管)是一种高功率半导体器件,基于门极可关断晶闸管(GTO)的原理,但通过集成门极驱动单元和优化结构(如硬驱动技术),实现了更低的导通损耗、更快的开关速度和更高的可靠性。它适用于中高压大功率应用,如高压直流输电(HVDC)、中压变频器和模块化多电平变换器(MMC)。IGCT 的结构为四层 p-n-p-n 器件,门极电流控制导通和关断,阳极电流在关断时快速换流至门极,通常在 1 μs 内完成。

专注于前沿技术的探索与实践,热衷于从代码逻辑、架构设计到行业应用等多维度深挖技术本质。期待在技术之路上与你共同成长~ 233

C/C++编程语言中typedef关键字介绍

typedef为C/C++编程语言的关键字,作用是为一种数据类型定义一个新名字,这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。typedef本身是一种存储类的关键字,与auto、extern、static、register等关键字不能出现在同一个表达式中。

liitdar的博客 31万+

【从零开始学习 SystemVerilog】1.1、SystemVerilog 概述

【从零开始学习 SystemVerilog】1.1、SystemVerilog 概述

努力,加油每一天鸭~ 1037

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C/Cpp的 typedef

C/Cpp的 typedef参考: https://en.wikipedia.org/wiki/Typedef http://publications.gbdirect.co.uk/c_book/chapter8/typedef.htmlIndicating what a variable represents 指示一个新名字来代表typedef int km_per_hour; typedef

XXXXX的专栏 899

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

C/C++ typedef用法!

引言 typedef 声明,简称 typedef,为现有类型创建一个新的名字。比如人们常常使用 typedef 来编写更美观和可读的代码。所谓美观,意指 typedef 能隐藏笨拙的语法构造以及平台相关的数据类型,从而增强可移植性和以及未来的可维护性。 在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。 typedef的...

HUYA69的博客 755

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

C/C++ typedef的理解 typedef void (*pfunc)(int);你不会再错了

C/C++ typedef的理解 typedef void (*pfunc)(int); 初学时: typedef int XXX,给类型int取一个别名xxx,嗯我懂了,EZ。 一段时间过后: typedef void (*pfunc)(int); ???将void取别名(*pfunc)(int)?不对啊,网上搜一搜,原来是函数指针啊,理解不了,先记住吧。 一段时间后: typedef int a[10]; ???又记? 关于typedef的解释 个人见解: typedef的作用是将 变量名 (声明变量这

qq_41191968的博客 5649

C/C++ typedef的理解 typedef void (*pfunc)(int);

C/C++ typedef的理解 typedef void (*pfunc)(int); 初学时: typedef int XXX,给类型int取一个别名xxx,嗯我懂了,EZ。 一段时间过后: typedef void (*pfunc)(int); ???将void取别名(*p...

qq_42174306的博客 1402

C/C++中typedef详解与使用示例

除了可以直接使用C/C++提供的标准类型名(如int、char、float、double、long等)和自己声明的结构体、共用体、指针、枚举类型外,还可以用typedef声明新的类型名来代替已有的类型名。

昊虹AI笔记 1776

typedef在C/C++的用法

在定义复杂数据结构(如指针、数组、结构体、联合体等)时,

m0_62342492的博客 1367

C/C++ typedef用法详解

第一、四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象。比如: char* pa, pb; // 这多数不符合我们的意图,它只声明了一个指向字符变量的指针, // 和一个字符变量; 以下则可行: typedef char* PCHAR; // 一般用大写 PCHAR pa, pb; // 可行,同时声明了两个指向字符变量的指针 虽然: char ...

jacklin_01的博客 175

C/C++ typedef

body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;} th{border: 1px solid gray; paddi...

dfuw13072的博客 96

C/C++中typedef关键字用法总结

首先介绍下typedef,它是一个关键字,可以理解为用于将数据类型起一个别名,但是与#define不同的是,typedef是编译期间由编译器进行处理的,而#define是一个预编译指令,是在预编译期由预处理器进行处理的。

Kellery 1126

C/C++ typedef用法

本文转载自https://www.cnblogs.com/vin666/p/6064339.html C/C++ typedef用法 引言 typedef 声明,简称 typedef,为现有类型创建一个新的名字。比如人们常常使用 typedef 来编写更美观和可读的代码。所谓美观,意指 typedef 能隐藏笨拙的语法构造以及平台相关的数据类型,从而增强可移植性和以及未来的可维护性。 在编程...

weixin_42143003的博客 130
上一篇: vim补全
下一篇: wiki--typedeef
心若天府
博客等级 码龄15年 37粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值