C语言指针——数组做函数参数(冒泡排序)

要点:数组做函数参数时退化为指针

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>




//数组做函数参数退化为指针
//打印
void printf_array(int *a, int len)
{
    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%d ", a[i]);
    }
}


//sorting,parameter(数组退化为指针,长度)
//双for循环,
void sorting_array(int *a, int len)
{
    int i = 0, j;
    int tmp;
    for (i = 0; i < len; i++)
    {
        for (j = i + 1; j < len; j++)
        {
            if (a[i] > a[j])
            {
                tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
            }
        }
    }
}



int main()
{
    int a[] = { 121, 22, 54, 43, 6, 88, 10 };

    int a_len = sizeof(a) / sizeof(a[0]);
    printf("%d\n", a_len);
    //Before the sorting
    printf("Before the sorting: ");
    printf_array(a, a_len);

    //sorting
    sorting_array(a, a_len);

    //After the sorting
    printf("\nAfter  the sorting: ");

    printf_array(a, a_len);

    printf("\n");

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a6taotao/article/details/80205817