C language address transfer and value transfer

Source: Internet
Author: User

1. in C language, all data parameters in non-array form are called in the form of value transfer. In the process of value transfer, parameters in the form of called functions are regarded as local variables of the function, that is, opening up space in the memory stack to store the values of real parameters put in by the main function, this becomes a copy of the real parameter. Therefore, the called function cannot modify the value of the actual variable as the real parameter, but can only modify the backup that is passed to it.

2. In the address transfer process, although the parameter of the called function is also used as a local variable, the memory space is opened up in the stack, however, the address of the real parameter variable put in by the main function is stored, and any operations of the called function on the form parameter are processed as indirect addressing, that is, access the real variable in the main function through the address stored in the stack. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.

3. At the same time, there is another issue worth noting: An array cannot be passed to a function.

Verify that:

 
Void fun (char a [10]) {int I = sizeof (a); char c = A [3];}

If the array is actually passed into the function, the value of I should be 10. Unfortunately, the value of I is 4, and it is not passed.

In C, when a one-dimensional array is used as a function parameter, the compiler always parses it into a pointer pointing to its first element address.

For the above value transfer and address transfer, the following usesCodeNotes:

 
/** Main. c *** created on: Apr 28,201 2 * Author: Root */# include <stdio. h> void swap_val (int x, int y) {int temp; temp = x; X = y; y = temp; printf ("x = % d, y = % d \ n ", x, y);} void swap_addr (int * X, int * Y) {int temp; temp = * X; * x = * Y; * Y = temp;} int main (void) {int A, B; printf ("Please input two integer \ n"); scanf ("% d ", & A, & B); if (a <B) {swap_val (a, B); printf ("A = % d, B = % d \ n",, b); swap_addr (& A, & B); printf ("A = % d, B = % d \ n", a, B);} return 0 ;}


The verification result is as follows:

Please input two integer
1
2
X = 2, y = 1
A = 1, B = 2
A = 2, B = 1

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.