A C-language one-dimensional array is passed to the function as a parameter:

Source: Internet
Author: User

Today encountered a one-dimensional array to do function parameters, then grilled the problem:


First, the conclusion:

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

2: The actual passed array size has no relation to the size of the array specified by the function parameter.

Then illustrate:

The following is an element exchange function that swaps the array "I" and the "J" of the arrays. Notice how the array is passed to the function.

Correct notation 1:


Explanation: The compiler parses an array into pointers to the shaping elements, that is, the first address of the array, with no specified number in the square brackets, because the compiler does not look at all, so it is best not to write, so as not to cause misunderstanding.


Correct wording 2:


Explanation: This is more straightforward to write, the parameter is a pointer to the shaping element, and the name of the array is the address of the first element, also confirms that the array name is a pointer constant.


There may be some classmates on the function of Array[i], Array[j] do not understand, in fact, array[i] = * (array+i), explaining that the first address plus the offset address I, is pointing to the first element.

Array (i) = (array +i) // This is a pointer to the first element.

Array[i] = * (array+i) // This is the first element of the array.

Notice () and [], don't look wrong.

Finish.





A C-language one-dimensional array is passed to the function as a 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.