二级指针做输入,此时交换的是指针指向的内存
#include<stdio.h> #include<stdlib.h> #include<string.h> void printMyarray(char myArray[10][30],int num) { int i; for(i = 0; i < num; i++) { //printf("%s \n",myArray[i]); printf("%s \n",*(myArray+i)); } } void sortMyarray(char myArray[10][30],int num) { int i = 0, j = 0; char tmp[30]; for(i = 0; i < num; i++) { for(j = i; j < num; j++) { if(strcmp(myArray[i],myArray[j]) > 0) { strcpy(tmp,myArray[i]); strcpy(myArray[i],myArray[j]); strcpy(myArray[j],tmp); } } } } int main() { int i = 0, j = 0; int num = 0; // char *myArray[] = {"aaaaa","bbbbb","ccccl","1111111"}; char myArray[10][30] = {"aaaaa","bbbbb","ccccl","1111111"}; num = sizeof(myArray)/sizeof(myArray[0]); printMyarray(myArray,num); sortMyarray(myArray,num); printf("after sort\n"); printMyarray(myArray,num); return 0; }
原文地址:https://www.cnblogs.com/wanghao-boke/p/11625369.html
时间: 2024-10-08 18:10:10