[c]代码库
/* 使用字符指针数组对字符串进行排序 */
#include <stdio.h>
#define NUM 5
int main(void) {
char * p[NUM] = { NULL };
int i, j;
char * tmp = NULL;
/* 数组内的有效字符指针个数必须等于NUM */
p[0] = "Nelson Aldrich";
p[1] = "A. Piatt Andrew";
p[2] = "Frank Vanderlip";
p[3] = "Henry P.Davison";
p[4] = "Charles D.Norton";
/* 对数组中的字符串排序 */
for (i = 0; i < NUM - 1; ++i) {
for (j = i + 1; j < NUM; ++j) {
if (0 < strncmp(p[i], p[j])) {
/* 互换两个指针指向的字符串 */
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
/* 输出数组中的各字符串 */
for (i = 0; i < NUM; ++i)
puts(p[i]);
return 0;
}