The parameters of C language function and the way of transmission

Source: Internet
Author: User

1. Formal parameters and actual parameters 1.1 form parameter parameters appear in the function, can be used throughout the function body. The formal parameter compiles the system without allocating storage space when it is defined, and only allocates the memory element when the function is called. The calling end memory unit is freed, so the formal parameter is valid only when the function is called and cannot be reused at the end of the call. 1.2 The actual parameter arguments appear in the key function, and when the function is called, the inking function transmits the values of the arguments to the parameters of the modulated function, thus realizing the data transfer between functions. There are two ways of passing: value passing and address passing. 2. Variables as function parameters when a parameter is defined as a variable, the argument can be a constant, a variable, and an expression, and the arguments between the functions are passed as values. The characteristic of value passing is "one-way transfer" of parameters;
int swap (int a,int b) {   int temp;   Temp=a;   a=b;   B=temp;   return 0;}  int main (void) {int A=3,b=4;swap (a, b);}
Because it is a value pass, one-way delivery does not change the value of a, B. 3. Array as function parameter 3.1 array element as function parameter array element is also called subscript variable, it has all nature of ordinary variable, so the array element as the argument of the function of the data transfer is no different from the ordinary variable, is also the value of passing
int swap (int a,int b) {   int temp;   Temp=a;   a=b;   B=temp;   return 0;}  int main (void) {int a[]={3,4};swap (a[0],b[0]);}

The value is also passed without changing the value of a[0]. 3.2 One-dimensional array name as function parameter array name is an address, is the first address of the array, so the array name as a function of the parameters of the data transfer, the implementation of the address delivery method. The so-called address delivery, as the name implies, is not the data itself, but the address of the data exists. When the function is called, the array name is the first address of the array is passed as an argument to the formal parameter (must be an acceptable address of the array name or pointer variable), the shape parameter group name after the first address has a real array, when the actual argument and formal parameters are the same number of groups, point to the same storage space, the change of the parameters So the method of communication can be regarded as the "two-way transmission" of data. 3.3 Array pointers, that is, the address of an array element as a function parameter due to the nature of the address of the array element is still the address, so it belongs to the address delivery method.
int swap (int *a,int *b) {   int temp;   Temp=*a;   *a=*b;   *b=temp;   return 0;}  int main (void) {int arr[] = {1,2};int *a = &arr[0];    int *b = &arr[1];swap (A, b);}

Focus:
    • The data passing of an array element (subscript variable) as an argument of a function is the way the value is passed, the array name (the first address of the array), the address of the array element (&arr[0]), and the data passing as a function parameter is the way the address is passed.
    • An argument is an array name, and there can be three forms when the parameter is received: The name of the list with the subscript (arr[0]). A pointer variable name (*A) that does not have an indexed array name (ARR) and a value for the receive address. Because the parameter group and the parameter group all point to the same paragraph, so they operate the same batch of data, so the change of formal parameter is changing the data in the argument.

The parameters of C language function and the way of transmission

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.