typedef int Int A10 Int A a Int

  • Slides: 37
Download presentation

定义数组类型 typedef int Int. A[10]; Int. A a; Int. A b[5]; Int. A *ap;

定义数组类型 typedef int Int. A[10]; Int. A a; Int. A b[5]; Int. A *ap; 定义了一个行指针 等价于: int a[10]; int b[5][10]; int (*ap)[10]; 13

自定义类型-typedef语句 typedef 原类型名 新类型; � � � typdef double Real; typdef char Byte; typdef

自定义类型-typedef语句 typedef 原类型名 新类型; � � � typdef double Real; typdef char Byte; typdef int *Pointer; � � � Pointer p int * p; Pointer *q int **q; 定义了一个二级指针 typdef int Array[10]; � � Array a int a[10]; Array *p int (*p)[10]; 定义了一个面指针 14

Typedef常用语结构类型 使用方便 � struct _POINT { float x, y; }; typedef struct _POINT; �

Typedef常用语结构类型 使用方便 � struct _POINT { float x, y; }; typedef struct _POINT; � � � typedef struct { float x, y } POINT; � � POINT p 1, p 2; struct _POINT p 1, p 2; POINT *p struct _POINT *p; 无名结构 如果结构成员多,一般写成多行 typedef struct { float x; float y; } POINT; 15

typedef多个新类型 � typedef struct { float x; float y; } POINT, * PPOINT; 定义了两个新的数据类型:POINT和PPOINT

typedef多个新类型 � typedef struct { float x; float y; } POINT, * PPOINT; 定义了两个新的数据类型:POINT和PPOINT x; x是什么类型? 16

typedef命令语法格式 类似于定义变量 � double Real; typedef double Real; � � � double *PReal; typedef

typedef命令语法格式 类似于定义变量 � double Real; typedef double Real; � � � double *PReal; typedef double *PReal; struct _Point; typdef struct _Point; � � Array a; int a[10]; int * PArray[20]; typedef int *PArray[20]; � � � Point p; struct _Point p; int Array[10]; typedef int Array[10]; � � Real x; double x; 这是指针数组 PArray p; int *p[20]; int (*AP)[30]; typedef int (*AP)[30]; � � 只是行指针 AP p; int (*p)[30]; 17

多级指针程序解析 程序 char *c[] = {"ENTER", "NEW", "POINT", "FIRST"}; char **cp[]= {c+3, c+2, c+1,

多级指针程序解析 程序 char *c[] = {"ENTER", "NEW", "POINT", "FIRST"}; char **cp[]= {c+3, c+2, c+1, c}; char ***cpp = cp; main() { printf("%s", **++cpp); printf("%s", *--*++cpp); printf("%s", *cpp[-2]+3); printf("%sn", cpp[-1]+1); }

多级指针程序解析 char *c[] = {"ENTER", "NEW", "POINT", "FIRST"}; char **cp[]= {c+3, c+2, c+1, c};

多级指针程序解析 char *c[] = {"ENTER", "NEW", "POINT", "FIRST"}; char **cp[]= {c+3, c+2, c+1, c}; char ***cpp = cp; 示意图: ccp cp cp[0] cp[1] cp[2] cp[3] c c[0] c[1] c[2] c[3] “ENTER ” “NEW” “POINT ” “FIRST ”

行指针(数组指针)[续] #include <stdio. h> #include <string. h> char (*defy(char *p))[5] { int i; for(i=0;

行指针(数组指针)[续] #include <stdio. h> #include <string. h> char (*defy(char *p))[5] { int i; for(i=0; i<3; i++) p[strlen(p)] = 'A'; return (char(*)[5])p+1; } void main() { char a[] = "FROGØSEALØLIONØLAMB"; puts( defy(a)[1]+2 ); } 31

如何更清晰地定义函数defy � � 函数defy的返回值是一个行指针, 基类型是:长度为 5的字符数组 利用typedef命令,程序更清晰 typedef char CA 5[5]; CA 5 *defy(char

如何更清晰地定义函数defy � � 函数defy的返回值是一个行指针, 基类型是:长度为 5的字符数组 利用typedef命令,程序更清晰 typedef char CA 5[5]; CA 5 *defy(char *p) { int i; for(i=0; i<3; i++) p[strlen(p)] = 'A'; return (CA 5*)p+1; } char (*defy(char *p))[5] { int i; for(i=0; i<3; i++) p[strlen(p)] = 'A'; return (char(*)[5])p+1; }