typedef定义数组类型示例如下:
typedef int sort[5]; // 声明sort为整型数组类型名
sort a={12,43,78,23,36}; // 定义a为整型数组名,它有5个元素
下面举一个例子,使用冒泡法对5个数从小到大进行排序。
#include <stdio.h>
typedef int sort[5];
int main(int argc, const char * argv[]) {
// insert code here...
sort a={12,43,78,23,36};
int temp;
for(int i=4;i>=1;i--)
for(int j=0;j<i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(int k=0;k<5;k++)
{
printf("%3d",a[k]);
}
printf("\n");
}
return 0;
}
代码运行结果:
/*
12 43 23 78 36
12 43 23 36 78
12 23 43 36 78
12 23 36 43 78
*/