A c array is degenerate into a pointer when it is a shape.

Source: Internet
Author: User

When an array is a function, the parameter is degraded to a pointer, and the size of the array cannot be evaluated directly.

Pass an array to a function. The array type itself is actively converted to a pointer type, so the actual pass is the address.


void func (int array[10])
void func (int array[])
void func (int *array)
So the above three function declarations are completely equal.


The reality is. Array to do the number of references. It is completely impossible to pass by value.

This is determined by the implementation mechanism of the C + + function.

The usual way to calculate the size of an array is as follows:

int len = sizeof (arr)/sizeof (type)

Get the length of an array using the total length and the length of a single element

However, the length of sizeof (ARR) is always 4, which is the length of a pointer.

To do this, there are two ways to solve the problem:

Method 1.

When a parameter is passed, the length of the array is added directly to a number of parameters.

Method 2. (Is wrong)

int *p = a[0];
int ilen = 0;
while (P! = NULL)
{
? ? ilen++;
? ? *p++;
}

A c array is degenerate into a pointer when it is a shape.

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.