C language array (below)

Write a bubble sort function, the code is as follows:

void bubble_sort(int arr[],int sz)
{
    //确定冒泡排序的函数
    int i = 0;
    for ( i = 0; i < sz - 1; i++)
    {
        int flag = 1;//假设这一趟要排序的数值已经有序
        //每一趟冒泡排序
        int j = 0;
        for ( j = 0; j < sz - 1 -i; j++)
        {
            if (arr[j]>arr[j + 1])
            {
                int tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
                flag = 0;//本趟排序的数据其实不完全有序
            }
        }
        if (flag == 1)
        {
            break;
        }

    }
}
int main()
{
    int arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    int i = 0;
    int sz = sizeof(arr) / sizeof(arr[0]);
    //对arr进行排序,排成升序
    //arr是数组,我们对数组arr进行传参,实际上传递过去的是数组arr首元素的地址 &arr[0]
    bubble_sort(arr,sz);//冒泡排序函数
    for ( i = 0; i < sz; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}

It should be noted that arr is an array. We pass parameters to the array arr. In fact, what is passed is the address of the first element of the array arr & arr[0]. If you want to find the number of elements in the array, you must first ask for it outside Pass in again.


Array name The address of the first element of the array. (There are two exceptions)
1. sizeof (array name ) -the array name represents the entire array, and sizeof (array name) calculates the size of the entire array in bytes.
2. & array name, the array name represents the entire array, & array name, the address of the entire array is taken out.


Summary:
To distinguish whether we pass the address of the first element or the address of the entire array, the meaning of the two is different.
2021.1.27
Always a fool computer

Guess you like

Origin blog.51cto.com/15080720/2608828