Beginner's study-C-language function parameter passing detail-struct and array

Source: Internet
Author: User

There are two ways to use a struct as a function parameter in C language: The value of the transfer and the address.

1. When a value is passed, the struct parameter is copied, and the value of modifying the member of the struct parameter in the function body is actually the value of the member that modifies a temporary copy of the calling parameter, which does not affect the calling parameter. In this case, the program space and time efficiency will be affected by the copying of the structure parameters.
Example:

typedefstruct tagSTUDENT{     char name[20];     int age;}STUDENT;voidprintf(“stu.name=%s,stu.age=%d/n”,stu.name,stu.age);}

2. Passing pointers directly to the structure of the first address of the body, in the function body through the pointer reference struct members, you can have the structure of the parameters of the members of the actual effect. High efficiency, often used in large-scale projects, such as the famous open source framework Nginx for the use of structures is a good example. The example will be given at the end.

In the

C language, an array is typically passed as a function parameter, which is the first address of an array. Therefore, the transfer of the function parameters of the array is only the transfer of the address, that is, the first address of the real parameter group is given the shape parameter group name. Parameter group name after the first address is obtained, it is equal to having a real array. In fact, the shape parameter group and the real parameter group are the same array and have a memory space together. Examples also see the use of Nginx below.

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};

Finally, it is easy to understand and give the definition of ngx_module_t

typedef structNgx_module_s ngx_module_t;structngx_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;};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Beginner's study-C-language function parameter passing detail-struct and array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.