C++ 02 指针数组

 

 

#include <iostream>
#include <cstring>
using namespace std;


void sort( char *name[], int n){

    char *tmp;
    int i,j,k;
    for(i=0;i<n-1;i++){
        k=i;
        for(j=i+1;j<n;j++){
            char const *a=name[k];
            char const *b =name[j];
            if(strcmp(a,b)) k =j;
            if(k!=i){
                tmp =name[i];
                name[i]=name[k];
                name[k]=tmp;
            }
        }
    }
}


void print_str(char *name[],int n){
    int i=0;
    char *p;
    p=name[0];
    while(i<n){
        p=*(name + i++);
        cout <<i<<":"<< p << endl;
    }
}

int main()
{
    char *name[] = { (char*)"how", (char*)"are", (char*)"you",(char*)"你好"};
    int n =4;
    sort(name,4);
    print_str(name,4);
}

 

1:你好
2:you
3:are
4:how

 

posted @ 2022-02-09 13:50  方诚  阅读(30)  评论(0编辑  收藏  举报