21.typedef
来源:互联网 发布:js中获取json对象的值 编辑:程序博客网 时间:2024/06/06 18:07
一、typedef作用简介
//
// main.m
// typedef
//
// Created by amesty on 15-1-23.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
//1.给基本数据类型起别名
typedefint Integer;
typedef unsigned int UInterger;
typedef float Float;
//2.指针类型char *起别名为String
typedefchar *String;
//3.给结构体起别名可以使代码更加简洁明
// 定义一个结构体,顺便起别名
//typedef struct MyPoint {
// float x;
// float y;
//} Point; MyPoint可以省略
// 4.给指向结构体的指针起别名
//typedef Point *PP;
struct MyPoint {
float x;
float y;
};
// 起别名 typedef Point * PP; Point point = {10, 20};
//5.使用typedef给枚举类型起别名
// 定义枚举类型
enum Season {spring, summer, autumn, winter};
// 给枚举类型起别名
typedef enum Season Season;
// 定义一个sum函数,计算a跟b的和
int sum(int a,int b) {
int c = a + b;
printf("%d + %d = %d", a, b, c);
return c;
}
//6.给指向函数的指针起别名
typedef int (*MySum)(int,int);
int main(int argc,constchar * argv[])
{
@autoreleasepool {
Integer i = -10;
UInterger ui =11;
Float f =12.39f;
NSLog(@"%d,%d,%.2f",i,ui,f);
// 相当于char *str = "This is a string!";
String str ="This is a string!";
NSLog(@"%s", str);
typedefstructMyPoint Point;
Point p;
p.x =10.0f;
p.y =20.0f;
NSLog(@"p.x=%.2f,p.y=%.2f",p.x,p.y);
//指向结构题的指针类型为pp
typedefPoint * PP;
// 定义结构体变量
Point point = {10,20};
//定义指针变量指针中存结构体的地址
PP poing = &point;
//利用指针变量访问结构体成员
NSLog(@"x=%f,y=%f", poing->x, poing->y);
// 定义枚举变量
Season s =spring;
NSLog(@"%d",s);
// 定义一个指向sum函数的指针变量p
int (*p1)(int,int) =sum;
NSLog(@"%d", (p1)(4,5));
// 定义一个指向sum函数的指针变量p
MySum p2 =sum;
NSLog(@"%d", (p2)(4,5));
}
return0;
}
- 21.typedef
- typedef
- typedef
- typedef
- typedef
- typedef- -
- typedef
- typedef
- typedef
- typedef
- typedef
- Typedef
- typedef
- typedef
- typedef
- typedef
- typedef
- typedef
- hdu---1425 sort
- aip.jdk1.4 1.5(5.0) 1.6(6.0) 7.0 8.0特点比较与不同
- Spring加载方式ContextLoaderListener和ContextLoaderPlugIn
- A beautiful story about NYC weather
- Xcode6添加.pch,xcode6.pch
- 21.typedef
- 【Android开发经验】ViewHolder到底用什么修饰?static?final?static final?
- HTML本地存储之IndexedDB(1)
- Redis 集群方案
- 【Android开发经验】Android相关问题的好文章整理
- IOS代码快速注释插件
- 黑马程序员—03OC三大特征
- UILabel自适应宽度
- [leetcode] 学习记录——Factorial Trailing Zeroes