C语言学记 -- 用指针实现二维字符串数组的选排

2023.12.14

#include 
#include 

int main()
{
	int max = 0, i = 0, j = 0;
	char arr[10][20] = { 0 }, * arr_1[10] = {NULL}, * temp = NULL;
	for (i = 0; i < 10;fgets(arr[i], sizeof(arr[i]), stdin),i++);

    //用指针数组存储每一维的地址
	for (i = 0; i < 10; arr_1[i] = arr[i],i++);
    //通过指针交换实现排序
	for (i = 0; i < 9; temp = arr_1[9 - i], arr_1[9 - i] = arr_1[max], arr_1[max] = temp, i++)
		for (j = 1, max = 0; j < 10 - i; max = strcmp(arr_1[j], arr_1[max]) > 0 ? j : max, j++);
	for (i = 0; i < 10; printf("%s", arr_1[i]), i++);
	return 0;
}

你可能感兴趣的:(C语言,c语言,算法,排序算法)