C function call rules

Source: Internet
Author: User


Function calling rules in C language: All parameters are called by passing values. But what do we usually call address transfer? This is what we need to explain.

All parameters of Function C are passed in the "pass value call" method, which means that the function will obtain a copy of the parameter value. In this way, the function can safely modify the copy value without worrying about modifying the parameters actually passed to the calling program.

However, if the passed parameter is an array name and the subscript is used in the function to reference the parameter of the array, in this case, the number of elements in the array is actually modified by the array elements in the calling program. The function will access the array elements of the calling program, and the array will not be copied. This behavior is called "Address Transfer call ".

The behavior of array parameters seems to be in conflict with the calling rule for value passing. However, there is no conflict here: the value of the array name is actually a pointer, and a copy of the pointer is passed to the function. Subscript reference is actually another form of indirect access. It can perform indirect access operations on the pointer and access the memory location pointed to by the pointer. The parameter is actually a copy, however, the original array is accessed by indirect access operations on this copy.

The so-called address transfer call is to pass a pointer to the required element, and then execute indirect access operations on the pointer in the function to access the data. The array name as a parameter is a pointer, And the subscript reference actually executes indirect access.

In this case, what is the behavior of calling the array value transfer? A copy of the parameter is passed to the function (a copy of the pointer pointing to the starting position of the array), so the function can operate its pointer parameter freely, you don't have to worry about modifying the corresponding pointer as a real parameter.

Therefore, all parameters are passed by passing values.

A classic function that exchanges two integers (no effect) can be called by explicitly passing values.


void swap(int x,int y){       int temp;       temp = x;       x = y;       y = temp; }

This is an integer exchange with no effect: because it is actually a copy of the parameter, the original parameter value is not exchanged.

If you want to modify the above program, you can use a pointer to implement it:

void swap(int *x,int *y){     int temp;    temp=*x;    *x=*y;    *y=temp;}

To access the value of the calling program, we must pass the pointer to the variable we want to modify to the function. Then, the function must use an indirect access operation on the pointer to modify the variable to be modified.

In the main function, because the function expects to accept a pointer, we should call it in the following way: swap (& a, & B );




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.