zoukankan      html  css  js  c++  java
  • 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏

    C语言中结构体作为函数参数,有两种方式:传值和传址。

    1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数。在这种情况下,涉及到结构体参数的拷贝,程序空间及时间效率都会受到影响。
    例子:

    typedef struct tagSTUDENT{
         char name[20];
         int age;
    }STUDENT;
    
    void fun(STUDENT stu)
    {
     printf(“stu.name=%s,stu.age=%d/n”,stu.name,stu.age);
    }

    2.传指针时直接将结构体的首地址传递给函数体,在函数体中通过指针引用结构体成员,可以对结构体参数成员的值造成实际影响。效率高,常在大型项目中用到,如著名的开源构架Nginx中对于结构体的使用就是一个很好的例子。例子该会在最后给出。

    C语言中数组作为函数参数,一般传递的是数组的首地址。因此在数组名作函数参数时所进行的传送只是地址的传送, 也就是说把实参数组的首地址赋予形参数组名。形参数组名取得该首地址之后,也就等于有了实在的数组。实际上是形参数组和实参数组为同一数组,共同拥有一段内存空间。例子同样见下面Nginx的使用。

    static ngx_command_t ngx_http_mytest_commands[] = {
        {
            ngx_string("mytest"),
            NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
            ngx_http_mytest,
            NGX_HTTP_LOC_CONF_OFFSET,
            0,
            NULL
        },
        ngx_null_command
    };
    
    static ngx_http_module_t ngx_http_mytest_module_ctx = {
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL
    };
    
    ngx_module_t ngx_http_mytest_module = {
        NGX_MODULE_V1,
        &ngx_http_mytest_module_ctx,//结构体指针传递
        ngx_http_mytest_commands,//数组默认地址传递
        NGX_HTTP_MODULE,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NGX_MODULE_V1_PADDING
    };

    最后便于理解,给出ngx_module_t的定义

    typedef struct ngx_module_s      ngx_module_t;
    struct ngx_module_s {
        ngx_uint_t            ctx_index;
        ngx_uint_t            index;
    
        ngx_uint_t            spare0;
        ngx_uint_t            spare1;
        ngx_uint_t            spare2;
        ngx_uint_t            spare3;
    
        ngx_uint_t            version;
    
        void                 *ctx;
        ngx_command_t        *commands;
        ngx_uint_t            type;
    
        ngx_int_t           (*init_master)(ngx_log_t *log);
    
        ngx_int_t           (*init_module)(ngx_cycle_t *cycle);
    
        ngx_int_t           (*init_process)(ngx_cycle_t *cycle);
        ngx_int_t           (*init_thread)(ngx_cycle_t *cycle);
        void                (*exit_thread)(ngx_cycle_t *cycle);
        void                (*exit_process)(ngx_cycle_t *cycle);
    
        void                (*exit_master)(ngx_cycle_t *cycle);
    
        uintptr_t             spare_hook0;
        uintptr_t             spare_hook1;
        uintptr_t             spare_hook2;
        uintptr_t             spare_hook3;
        uintptr_t             spare_hook4;
        uintptr_t             spare_hook5;
        uintptr_t             spare_hook6;
        uintptr_t             spare_hook7;
    };

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    U盘文件被隐藏的解决办法
    Regex 符号说明
    HTML特殊字符编码对照表
    Regex 常用
    String decryption with de4dot
    使用Topshelf创建Windows服务[转载]
    de4dot Deobfuscator for .NET
    [原创]extjs htmleditor增加截图快速粘贴功能 插件
    de4dot FAQ
    Cache缓存
  • 原文地址:https://www.cnblogs.com/paulweihan/p/4660089.html
Copyright © 2011-2022 走看看