[Sorting] array parameters in function declaration

Source: Internet
Author: User

In the array parameter declared by the function, and specify the length of the array. In C, a single function is allowed to manipulate any lengthOne-dimensional array. The disadvantage of this feature is that the function cannot know the length of the array. If you really need to know the length of the array, you must pass it to the function as a separate parameter.

When the array name is used as a real parameter, the pointer passed to the function is actually a pointer to the starting position of the array.

/* Development Environment: vs2010 Window XP SP3 */# include <stdio. h> int main (void) {void array_len (int v []); int V [] = {70, 30, 40, 60, 10, 20, 50,100, 80, 90}; int Len; array_len (V); Len = sizeof (v)/sizeof (INT); // 40/4 = 10, sizeof (V) returns the total number of elements in a one-dimensional array: 10*4 printf ("% d \ n", Len); // 10 return 0 ;} void array_len (int v []) {int Len = sizeof (v)/sizeof (INT); // 4/4 = 1, sizeof (V) returns the length of the int * P pointer, which is 4 bytes printf ("% d \ n", Len); // 1}

The same result is returned when the int value is changed to long.

Note that:
Pointers and long int occupy the same word width, that is, the maximum addressing width of the machine, and 32 bits are 4 bytes.

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.