C language by value transfer mechanism and by reference transfer mechanism __c language

Source: Internet
Author: User

When calling a custom function, it is often encountered to pass arguments to the called function, and the parameters are passed by value and by reference.

Transfer by value (Pass-by-value mechanism): When transferring variables to a function, the variable value is not passed directly to the function, but a copy of the variable value is first made and stored on the stack, which is used to invoke the function instead of the variable itself. This method of calling a function cannot modify the value of the element.

By reference passing mechanism (pass-by-value Reference): This way is to take the address of the variable as a variable value, to pass the address to the function, it is only a copy of the address passed, not the initial address, but it is still an address, referencing the original variable, and the original address to point to the same memory, The function can directly access the original structure through this address (this should be why the pointer as a parameter, you can modify what the pointer points to). Library function scanf_s () The address as a parameter is the truth, this way the function can be modified to change the value of the element.

When an array is a variable, it simply passes a copy of the array address, passing the array name to the function, and because the array name refers to the starting address of the array, it can be handled by the reference pass-through mechanism when the array name is used as a parameter.

Note the difference between arguments and variables, eg: Declare a function as follows: int fun (int a,int *pnumber), where a and pnumber are parameters.

int main (void)
{    int b,c;
     b= Fun (int c,int *p);
    ////statements.
}

where C and P are variables, these two variables are mapped to parameters a and pnumber.

The following is an example of passing by value and passing by reference, as follows:

#include #include int sum (int numberAdd1);//function declaration int PointerAdd1 (int *number2);//function declaration int Array (int row[]);/function declaration.
When you pass the array name as a parameter, passing the pointer argument to the function, only the address copy of the array is passed, but the array is not passed.
 int main (void) {int i;
 int array[5]={1,2,3,4,5};
 int number=9;
 int *pnumber=&number;
 int number1;
 Number1=sum (number);
 printf ("The Number1 value is%d.\nthe nubmer value is%d.\n", number1,number);
 Number= PointerAdd1 (Pnumber);
 printf ("If transfer" The number,the value of number is%d.\n ", number);
 Array[5]=array (array);//The array name array is passed to the function array ().
	for (i=0;i<5;++i) {printf (' The value array[%-d] is%d.\n ', i,array[i]);
 System ("pause");
return 0;
 the int sum (int numberAdd1)//sum () function is defined. {numberadd1=numberadd1+1;///passing mechanism by value.
	 Verify that the parameter value plus 1 has no effect on the variable value.
 return NUMBERADD1;
	the int PointerAdd1 (int *number2)//POINTERADD1 () function is defined. {return (*number2+=1);//by reference passing mechanism.
	Verify that the parameter value plus 1 has no effect on the variable value.
	the int Array (int row[])//array () function defines {int i=0; for (i=0;i<5;++i) {*row=* (row++) +1;////is passed by reference. Verify that the parameter value plus 1 has no effect on the variable value. When an array is passed as a variable, an address is passed.
	return row[5]; }

Note: If you do not want the reference value of the address to be modified when the address is an argument, you can use the keyword const to modify the function arguments. This means that the function will refer to the variable value passed to the parameter as a constant, and the specified function cannot modify the value that the variable points to.
BOOL SendMessage (const char *pmessage)
{
//code to send the message.//at compile time, the compiler will confirm that the code in the function body does not modify the content that the pmessage points to. Make sure that
                           pointer changes to constants//data should be safe. If there is a modification, the compiler will make an error. return
true;
}




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.