C语言:通过指针对数组元素进行排序

//

//  main.c

//  Pointer_array

//

//  Created by ma c on 15/8/2.

//  Copyright (c) 2015年. All rights reserved.

//  要求:使用指针的指针输出字符串。首先要使用指针数组创建一个字符串数组,然后定义指向指针的指针,使其指向字符串数组,并使用其输出数组中的字符串。同时对数组中的内容进行升序排序。

 

#include <stdio.h>

#include<string.h>

void Ascsort(char **arr,int len)//接收指针类型的数组,以及长度

{

    for(int i=0;i<len-1;i++)

    {

        for(int j=0;j<len-1-i;j++)

        {

            if(strcmp(arr[j],arr[j+1])>0)//升序排序

            {

                char *temp = arr[j];

                arr[j] = arr[j+1];

                arr[j+1] = temp;

            }

        }

    }

}

void printarray(char* arr[],int len,char **p)

{

    for(int i=0;i<4;i++)

    {

        printf("%s ",*(p+i));

    }

    printf("\n");

}

int main(int argc, const char * argv[])

{

    char* str[]={"Jony","Tom","Smith","Boy"};//指针类型的数组

    char **p;//定义指向指针的指针变量

    p = str;

 

    printf("排序前:");

    printarray(str,4,p);

    

    Ascsort(str,4);

    

    printf("排序后:");

    printarray(str,4,p);

 

    return 0;

}

 

posted @ 2015-08-02 17:25  XYQ全哥  阅读(3271)  评论(0编辑  收藏  举报