Use array name as function parameter and variable name as function parameter

Source: Internet
Author: User

Use array name as function parameter and variable name as function parameter

When a C language calls a function, the actual and actual functions are combined using the "value transfer" method.Variable nameWhen used as a function parameter, the value of the variable is passed. When the array name is used as the function parameter, because the array name represents the address of the first element of the array, the passed value is the address, therefore, the parameter must be a pointer variable.

When the array name is used as the real parameter of the function, since the corresponding parameter is actually a pointer variable, why is the form of an array of parameters allowed?

This is because the subscript method and pointer method in the C language can access an array (if there is an array a, a [I] And * (a + I) are unconditionally equivalent ), the subscript representation is more intuitive and easy to understand. Therefore, many people are willing to use the array name as the form parameter to correspond to the real parameter array. From the application perspective, you can think that there is an array of form parameters, which gets the starting address from the real parameter array. Therefore, the form parameter array and the real parameter group occupy the same memory unit. During the function call, if the value of the form parameter array is changed, the value of the real parameter array is changed.

Note: The real parameter array name represents a fixed address, or a pointer constant, but the form parameter array name is not a fixed address, but is processed according to the pointer variable.

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.