//移植没用过c语言中的typedef,今天晚上自己试了一下。
#include <stdio.h>
#include <string.h>
typedef int count;
typedef void (typedef_fun)(void);
void typedef_fun_test(void)
{
puts("\nfun_model");
}
void typedef_fun_1(void)
{
puts("\nfun_1");
count i = 3;
count j = 4;
printf("i:%d\n",i);
printf("j:%d\n",j);
}
void typedef_fun_2(void)
{
puts("\nfun_2");
typedef int a[5];
typedef int * p;
a a1 = {1,2, 3, 4, 5 }, a2 = {5, 4, 3, 2, 1};
p p1 = a1, p2 = a2;
//p p1 = a1;
//p p2 = a2;
putchar((char)a1[3] + '0');
//putchar((char)a1[3]);
puts("");
printf("p1[2]:%d\n", p1[2]);
printf("*(p2 + 4):%d\n", *(p2 + 4));
}
void typedef_fun_3(void)
{
puts("\nfun_3");
typedef int b[5];
typedef int (*c)[5];
b b1 = {2, 3, 4, 5, 6};
c c1 = &b1; //c c1 = b1; warning
printf("c1[2]:%d\n", *(c1[0] + 2));
}
void typedef_fun_4(void)
{
puts("\nfun_4");
typedef int b[5];
typedef int *c[10];
b b1, b2, b3, b4, b5;
c c1 = {b1, b2, b3, b4, b5};
*(c1[0]) = 2;
*(c1[1] + 1) = 3;
*(c1[2] + 2) = 4;
c1[3][3] = 5;
*(c1[4] + 4) = 6;
printf("b1[0]:%d\n", b1[0]);
printf("b2[1]:%d\n", b2[1]);
printf("b3[2]:%d\n", b3[2]);
printf("b4[3]:%d\n", b4[3]);
printf("b5[4]:%d\n", b5[4]);
}
void typedef_fun_5(void)
{
puts("\nfun_5");
typedef char d[8][10];
typedef char *e[10];
d d0 = {1}, d1 = {1}, d2= {2}, d3 = {3};
e p = {d0[0], d1[0], d2[0], d3[0]};
p[0][0] = 5;
p[1][1] = 4;
p[2][2] = 3;
p[3][3] = 2;
printf("d0[0][0]:%d\n", d0[0][0]);
printf("d1[0][1]:%d\n", d1[0][1]);
printf("d2[0][2]:%d\n", d2[0][2]);
printf("d3[0][3]:%d\n", d3[0][3]);
}
void typedef_fun_6(void)
{
typedef struct {
char *name;
int length;
int age;
}zq;
zq zq1, *zq2;
zq2 = &zq1;
zq1.name = "zhangquan";
zq1.length = 175;
zq1.age = 25;
printf("sizeof(zq):%d\n",sizeof(zq));
printf("zq2->name:%s\n", zq2->name);
printf("zq2->length:%d\n", zq2->length);
printf("zq2->age:%d\n", zq2->age);
}
void typedef_fun_7(void)
{
typedef_fun *typedef_test[] = {
typedef_fun_test,
typedef_fun_1,
typedef_fun_2,
typedef_fun_3,
typedef_fun_4,
typedef_fun_5,
typedef_fun_6,
NULL
};
typedef_fun **fun_p = typedef_test;
(*fun_p)();
typedef_test[1]();
(*(fun_p + 2))();
//for (; (*fun_p)(); ++fun_p);//error
for (;*fun_p; ++fun_p)
(*fun_p)();
}
void typedef_fun_8(void)
{
void (*test_fun_p)(void) = typedef_fun_test;
test_fun_p();
(*test_fun_p)();
(test_fun_p)();
}
//#define TYPEDEF
int main(int argc, char **argv)
{
#ifdef TYPEDEF
typedef_fun_test();
typedef_fun_1();
typedef_fun_2();
typedef_fun_3();
typedef_fun_4();
typedef_fun_5();
typedef_fun_6();
#else
typedef_fun_7();
typedef_fun_8();
#endif
return 0;
}
//Makefile
TARGET := typedef
SOURCE := typedef.c
$(TARGET):$(SOURCE)
@gcc $< -o $@
./$@
@echo "" >> $<
clean:
rm $(TARGET) -rf