zoukankan      html  css  js  c++  java
  • 指针作为函数参数

    演示结果1:

    演示结果2:

    示例代码:

    #include <iostream>
    
    
    void swap(int *,int *);
    void swap1(int *,int *);
    void function1(void);
    void displayIntArray(int *,int);
    void bubbleSort(int *,int);
    void function2(void);
    int main(int argc, char** argv) {
        //function1();
        function2(); 
        return 0;
    }
    
    /**
        演示排序 
    */
    void function2(void){
        int array[10]={9,1,2,5,6,3,4,8,7,10};
        printf("原来的数组: "); 
        displayIntArray(array,10);
        bubbleSort(array,10);
        printf("排序后数组: ");
        displayIntArray(array,10);
        
    }
    
    /**
        冒泡排序 
    */ 
    void bubbleSort(int *array,int arrayLength){
        for(int i = 0;i<arrayLength;i++){
            for(int j = 1;j<arrayLength-i;j++){
                if(array[j]<array[j-1]){
                    swap1(&array[j],&array[j-1]);
                }
            }
        } 
    }
    
    
    /**
        展示数组 
    */ 
    void displayIntArray(int *a,int arrayLength){
        
        for(int i = 0;i<arrayLength;i++){
            printf("%d ",a[i]);
            /*
            五个数字 换一行 
            if((i+1)%5==0){
                printf("
    ");
            }
            */
        }
        printf("
    ");
        
    }
    /**
        演示换位
    */ 
    void function1(void){
        int a = 0xffffffff;
        int b = 0xfffffffe;
        printf("a = %d,b = %d
    ",a,b);
        swap1(&a,&b);
        printf("a = %d,b = %d
    ",a,b);
        
        int c = 0x7fffffff;
        int d = 0x00000001; 
        printf("c = %d,d = %d
    ",c,d);
        swap1(&c,&d);
        printf("c = %d,d = %d
    ",c,d);
    }
    
    /**
        交换算法1 
    */ 
    void swap(int *a,int *b){
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    /**
        交换算法2 
    */
    void swap1(int *a,int *b){//两数之和大于0xffffffff 应该是也可以的。 
        *a = *a + *b;//10 9 
        *b = *a - *b;//10 1
        *a = *a - *b;
    }
  • 相关阅读:
    CSS盒子模型
    getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
    MYSQL中的CASE WHEN END AS
    单点登录的精华总结
    git&github
    June 21st 2017 Week 25th Wednesday
    June 20th 2017 Week 25th Tuesday
    June 19th 2017 Week 25th Monday
    June 18th 2017 Week 25th Sunday
    June 17th 2017 Week 24th Saturday
  • 原文地址:https://www.cnblogs.com/letben/p/5216052.html
Copyright © 2011-2022 走看看