How can I find the length of any int array in C?

Source: Internet
Author: User
How can I find the length of any int array in C? Source: csdn Author: bmcrnet Release Date: popularity: 983

Problem: int length (INT ar []); only the first address of the array is given, and the length of the array is returned.
The operations on arrays in C language are not flexible, and array operations are indispensable.

Computing memory capacity
Use
The sizeof operator can calculate the size of the array (number of bytes ). In Example 7-3-3 (a), the value of sizeof (a) is 12 (do not forget '/0 '). Pointer P points to a,
The value of sizeof (P) is 4. This is because sizeof (p) obtains the number of bytes of a pointer variable, which is equivalent to sizeof (char *) rather than the memory capacity referred to by P.
C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory.

Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type. In Example 7-3-3 (B), sizeof (a) is always equal to sizeof (char *) regardless of the size of array *).

Char A [] = "Hello World ";
Char * P =;
Cout <sizeof (a) <Endl; // 12 bytes
Cout <sizeof (p) <Endl; // 4 bytes
Example 7-3-3 (a) Calculate the memory capacity of the array and pointer

Void func (char a [1, 100])
{
Cout <sizeof (a) <Endl; // 4 bytes instead of 100 bytes
}
Example 7-3-3 (B) the array degrades to a pointer

The length of the array can only be passed as a parameter.

Of course, you can also do this:
# Include "stdio. H"

Typedef struct array_box
{
Int array [10];
} Array_box;

Void getdata (array_box * arraybox_p)
{
Int COUNT = sizeof (arraybox_p-> array)/sizeof (INT );
Int I;
For (I = 0; I <count; I ++)
{
Arraybox_p-> array [I] = I;
}
}

Main ()
{
Array_box arraybox;
Getdata (& arraybox );
}

To create a struct with only one element of the array type, it is equivalent to packing the array with a box, occupying the same memory space, but it can achieve the desired effect.

If you want to know the actual length of the array, there is no way to do it. You only need to count it ~~

1 The size of the C-style array can be calculated by sizeof during definition, for example:

# Define ar_size (a) sizeof (a)/sizeof (A [0])

Char STR [] = "some thing for test ";
Int A [] = {1, 3, 4 };
Int B [20];

Ar_size (STR); // 20
Ar_size (a); // 3
Ar_size (B); // 20

Note that sizeof is a static evaluation between compilers. It must know the exact type of the object.

2 arrays serve as function parameters and serve as pointers. The function only transmits a 4-byte address without its size information.

Upstairs justnewbie (laziness + pride + impatience !) This is incorrect.

Int length (INT ar [])
{
Sizeof Ar; // always 4 (of course, 2 in a 16-bit compiler)
}

3. c99 supports dynamic arrays. You can check the data and it seems that automatic size setting is supported. However, few compilers support c99 currently. I have not tried it.

There is no way.
When you define an array:
Int A [] = {1, 2, 3}; // actually compiled as int A [3] = {1, 2, 3}

The array name represents the address of the array. Note-you absolutely cannot dynamically obtain the array size through the array name. When you lose the length of a, you will never know its length.

So what is sizeof? Didn't he get the size of a through the name of? -- A big mistake!

The sizeof keyword generates a constant during the compilation period (Note 1). Its operation method is as follows:

When you write:
Sizeof
The essence is:
Sizeof (type of)

What is the type of? The compiler finds that the definition of a is int [3].
That is to say, sizeof A is actually:
Sizeof (INT [3])
It is equivalent to constant 12 (assuming that Int Is 4 bytes ).

Consider a function
Void func (int A []);
// There is no essential difference between writing int A [3] -- maybe you should try writing int (& A) [3]?

C ++ specifies that when an array is used as a form parameter, a represents the first address of the array.
Its underlying significance is that a degrades to a 4-byte pointer, and no variable indicates that the size of the array will be "automatically" passed in.

Let's take a look at what sizeof A is at this time:
Sizeof (A []) = sizeof (int * const) = 4 // Of course a [] is not a legal C ++ type

Still not convinced? Okay -- let's ask another question: if you are a C/C ++ designer, how do you enable void func (int A []) on the basis of compatibility with the original design? what is the transfer address and size?

First, a is a variable and is similar to an array. It must be an address; otherwise, you do not know how to index the element.
How can he bring another variable to indicate his size?

Expand sizeof (a) capabilities?

Sizeof A must generate code-whether it is a constant or something. To let him determine the value of a at runtime, a must carry his size information.

1. You must modify the C standard so that C supports the "two" arrays. One is the array at the definition, which allocates large contiguous memory, which is the same as the original C standard.

2. An array is passed as a parameter. You must pass the address and array size. This array is actually an 8-byte Structure {address; size} (in fact, it may be more complicated. How can we implement the multi-weft array? )

3. The system must implement its [], *, and & based on two different arrays. The original array is addressable based on its first address offset (this is a constant) and subscript. The parameter array first obtains the "Address" content (this is a variable ), then address the address ....

Er... consider multi-dimensional arrays again -- doesn't that sound like a complete vector model?

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.