The array is automatically converted to the same type of pointer __ function as a function parameter

Source: Internet
Author: User

High-quality C + + programming guidelines indicate that when an array is passed as a function parameter, the array is automatically degraded to the same type of pointer, which is correct for a one-dimensional array (currently considered correct), but for multidimensional arrays, this is clearly not exactly true. The C language takes an array parameter as a pointer because of efficiency, and if you copy an array all this is a loss of performance. So at the moment, whether you write in a function declaration like ' void Func1 (char a[]) ', or like ' void Func1 (char *a) ', the compiler will look at it as the latter form.
So for a regular examination of the written questions:
Void f (Char a[])
{
Cout<<sizeof (a) <<endl;
}
The output is naturally 4. (The default pointer occupies 4 digits, followed by this standard)
For two-dimensional array situations, two-dimensional arrays can be represented in several forms:
(1) Char A[m][n]--standard form;
(2) Char *p[n]--pointer array form;
(3) char (*P) [n]--array (row) pointer form
(4) Char **p--the form of a pointer pointer
Although these forms can represent two-dimensional arrays, they are not equivalent, and the transformation of two-dimensional arrays as parameters has a principle to follow. is to make the length of the array rows still recognizable in the child function.
(1) Char a[m][n]--void func (char (*p) [n]); It can also be written as void Func (char p[][n]) (the compiler automatically translates char p[][n] to char (*P) [n]), so this time sizeof (p) =4;sizeof (*P) is not 4 ha ...
(2) Char *a[n]--void func (char *p[]); it can also be written as void func (char **p), when sizeof (p) =4,sizeof (*p) = 4;
(3) char (*a) [n]--void func (char (*p) [n]); At this time sizeof (p) =4,sizeof (*p) =n;
(4) Char **a--void func (char **p);
In fact, 2 is a one-dimensional array of cases, 3 is a one-dimensional array pointer, 4 is a two-level pointer delivery.
The above * and [] interchangeable, written p[] situation can be changed to *p,*p can be changed to p[],
When you can write as p[], writing to any number of cases in [] does not affect the program. The compiler converts this situation to a *p case.
The general rule is that when you pass the array name in, you must allow the computer to recognize the length of the trip (the array is two-dimensional). The three-dimensional situation is similar, find the law, all cases can be solved.
Note: All the cases listed above are tested and passed under vc6.0.
Article Source: Fei Nuo network (www.firnow.com): http://dev.firnow.com/course/3_program/c++/cppjs/2007925/73641.html

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.