指针数组的应用

问题描写叙述
在主函数中输入10个长度不超过10的字符串,用还有一个函数对它们排序,然后在主函数输出这10个已排好序的字符串。

要求用指针数组来处理

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
void sort(char *p[],int n){

    int i,j,k;
    char temp[10];
    for(i=0;i<n-1;i++){
        k=i;
        for (j = i+1; j <n; j++)

            if (strcmp(p[j],p[k])<0)
            {
                k=j;
            }
            if (k!=i)
            {
                strcpy(temp,p[i]);
                strcpy(p[i],p[k]);
                strcpy(p[k],temp);
            }

}
}
void main(){

    int i;
    char *p[10],str[10][10];
    cout<<"inputs 10 strings:"<<endl;
    for ( i = 0; i <10; i++)
    {
        cin>>str[i];
        p[i]=str[i];
    }
    sort(p,10);
    cout<<"Now,the sequence is:
";
    for (i = 0; i < 10; i++)
    {
        cout<<p[i]<<endl;
    }
}
原文地址:https://www.cnblogs.com/zfyouxi/p/5059493.html