Array Length Calculation

Source: Internet
Author: User

(1) sizeof
Method: sizeof (array name)/sizeof (array type name)
Note: If the byte occupied by the array is divided by the byte occupied by the array type, the result is the number of array elements.
(2) strlen
Description: strlen, returns the Valid String Length.
Method: strlen (character array name) // returns the valid character length of the character array, excluding '/0' at the end'

Note:
When the array is passed as the number of functions, the array name represents the first address of the array instead of the array content, so sizeof and strlen cannot be used;
Therefore, when transferring an address, provide two numbers of records: one is the array name, representing the first address of the array; the other is the number of array elements to determine the number of passes. For example: # include <iostream>
Using namespace STD;
Void sum (INT array [])
{
Cout <"sizeof (array) =" <sizeof (array) <Endl;
}
Void main ()
{
Int A [] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Cout <"sizeof (A) =" <sizeof (a) <Endl;
Sum ();
} Calculation result: Sizeof (A) = 40
Sizeof (array) = 4
Analysis: When the array is passed as the number of functions, the array name represents the first address of the array, that is, the pointer, rather than the array content. If the entire array is passed, stack overflow will occur. Therefore, in the main function, sizeof is used to calculate the exact array length. In the call function, because the transmitted array is no longer the array itself, but its address, it is actually the length of the array address calculated using sizeof. In this case, sizeof (array ), it is actually sizeof (INT ).

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.