• 函数传参


    1  函数传参的顺序:

    #include <stdio.h> 
    //进栈栈地址是递减的。先进栈的在高地址,后进栈的在低地址。

    //函数传参:参数从最右边先进栈,先进后出。
     
    #include <stdio.h> 
    void fun(int a, ...) 
    { 
        int i;
        int *temp = &a;
        temp++;
        for (i = 0; i < a; ++i) 
        { 
            printf("%d
    " , *temp);
            temp++; 
        } 
    }
    void funstr(int a, ...) 
    { 
        int i;
        int *temp = &a;
        temp++;
        for (i = 0; i < a; ++i) 
        { 
            printf("%s" ,  (char*)(*temp));
            temp++; 
        } 
        printf("
    ");
    }
    int main() 
    { 
        int a = 12; 
        int b = 22; 
        int c = 32; 
        int d = 42; 
        fun(4, a, b, c, d); 
    
    
        char *as = "I ";
        char *bs = "am " ;
        char *cs = "fire!" ;
        funstr(3 , as ,bs,cs);
        return 0; 
    } 
    /*
    gcc for.c ;./a.out 
    12
    22
    32
    42
    I am fire!
    */

     2 拷贝传递

    typedef struct {
        char a;
        char b ;
    }ST;
    int fun2(ST st){
        printf("in fun2 : 0x%x
    ",&(st.a));    
        printf("in fun2 : 0x%x
    ",&(st.b));
    }
    int main()
    {
        ST st ;
        printf("0x%x
    ",&(st.a));    
        printf("0x%x
    ",&(st.b));
        fun2( st);
    }

    /*

    0xbfc2841e
    0xbfc2841f
    in fun2 : 0xbfc28400   //在函数内外的st内容相等,地址改变(值传递,拷贝传递)。占据堆栈中sizeof(ST)的内存。

    in fun2 : 0xbfc28401

    */

  • 相关阅读:
    Poj3678:Katu Puzzle
    2-SAT
    Bzoj3238: [Ahoi2013]差异
    expressJS
    expressJS
    expressJS
    [转]View属性 之 paddingStart & paddingEnd
    在Activity之间使用Intent传值和Bundle传值的区别和方式
    [转]Java初始化顺序总结
    final关键字修饰的变量
  • 原文地址:https://www.cnblogs.com/mylinux/p/3978855.html
Copyright © 2020-2023  润新知