Array name as function parameter

Source: Internet
Author: User

 Array name as function parameter

The differences between using array names as function parameters and using array elements as real parameters are as follows:
(1) When array elements are used as real parameters, as long as the array type is the same as the type of the parameter variable of the function, the type of the array element used as the subscript variable is also the same as that of the function variable. Therefore, the form parameter of the function is not required to be a subscript variable. In other words, array elements are treated as normal variables. When using array names as function parameters, both the form parameters and the corresponding real parameters must be arrays of the same type and must have clear array descriptions. An error occurs when the parameters and real parameters are of different types.

(2) When common variables or subscript variables are used as function parameters, the parameters and real variables are two different memory units allocated by the compilation system. When a function is called, the value passing is to assign the value of the real variable to the shape variable. When using the array name as a function parameter, it does not pass the value, that is, it does not assign the value of each element of the real parameter array to each element of the form parameter array. Because the actual parameter array does not exist, the compilation system does not allocate memory for the parameter array. How is data transferred? Because the array name is the first address of the array. Therefore, when using the array name as a function parameter, the transfer is actually the address transfer, that is, the first address of the real parameter array is assigned to the array name of the form parameter. After the array name of the form parameter obtains the first address, it is equal to the specific address. In fact, the form parameter array and the real parameter group are the same array, and a memory space is used together.

 
# Include <stdio. h> void change (int K []) {k [0] = K [5];} Main () {int X [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10}, n = 0; while (n <= 4) {change (& X [N]); N ++;} For (n = 0; n <5; n ++) printf ("% d", X [N]); printf ("\ n"); Return 0 ;}
Output result: 678910

When N = O, transfer the X [0] address to the callback function change, that is, the form parameter K and the real parameter X point to the same storage unit X [0], after the function is executed, K [0] = 6, that is, X [0] = 6: WHEN n = 1, K points to the unit of the 1st array elements in the X array, that is, K [0] and X [1] share the same storage unit. After the called function is executed, K [0] = 7, that is, X [1] = 7, the values from X [0] to X [4] are 6, 7, 8, 9, and 10 respectively.

Array name as function parameter

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.