Array name, array name address, and problems caused by function Transfer

Source: Internet
Author: User

// Array_para.cpp: defines the entry point of the console application.
//

// Array address and problems caused by function Transfer

 

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Void test (int A [5]);
Void Test2 (int A [5]);

Int _ tmain (INT argc, _ tchar * argv [])
{
Int A [5] = {1, 2, 3, 4, 5 };
Cout <"sizeof A is:" <sizeof (a) <Endl;
Test ();
Cout <Endl <"****** begin Test2 (a) ******" <Endl;
Test2 ();
Cout <Endl <"****** end Test2 (a) ******" <Endl;
Cout <Endl <"***** begin repeat content of Test2 in main function without passing array parameter ********" <Endl;
Printf ("(a) is: % d, (& A) is: % d \ n", (a), (& ));
Printf ("(a + 1) is: % d, (& A + 1) is: % d \ n", (a + 1 ), (& A + 1 ));
Printf ("* (a + 1) is: % d, * (& A + 1) is: % d \ n", * (a + 1 ), * (& A + 1 ));
Cout <Endl <"****** end repeat content of Test2 in main function without passing array parameter ********" <Endl;
While (1 ){};
Return 0;
}

Void test (int A [5])
{
Printf ("size of (a) as function parameter is % d \ n", sizeof (a); // result?
}

Void Test2 (int A [5]) // The parameter is of the INT (*) [5] type.
{
Printf ("(a) is: % d, (& A) is: % d \ n", (a), (& )); // In fact, here a is the form parameter int (* A) [5], and & A is the address of the form parameter, which is not the same as & A as the real parameter.
Printf ("(a + 1) is: % d, (& A + 1) is: % d \ n", (a + 1 ), (& A + 1 ));
Printf ("* (a + 1) is: % d, * (& A + 1) is: % d \ n", * (a + 1 ), * (& A + 1 ));
}

 

// Conclusion (refer to "Repeat Pointer" by Apsara Yu Jian stream ):

 

// 1: array name a is actually a symbolic constant, is a right value, is the first address of the array (for an address of the array type ), the value is equal to & A [0] (unlike a, & A [0] is an int * address ). A + I crosses the I sizeof (INT) and the calculation result is & A [I]

 

// 2: The [] symbol in a [1] is actually equivalent to * (a + 1)

 

// 3. For the combination of & * a, the compiler will optimize it to. Because & * offsets each other

 

// 4. Because the array name is the right value rather than the variable (the left value), obtaining the address & A for the array name is an operation that is not certain. C language standard allows the & operator to be used for any entity that can perform addressing operations, and sets it as the first address to get the array object, and has & A =

 

// 5: & A is of the INT (*) [5] type, which indicates the entire "array object". & A + 1 spans a sizeof (), equivalent to a [5]

 

// 6: When an array is passed as a parameter to a function, it degrades to a pointer (that is, the first address of the array is assigned to the INT (*) [5] type pointer as the parameter). Its size is 4.

// 7: After array name a is passed to the function as a parameter, function a actually refers to the form parameter int * a, and & A actually takes the address of the form parameter int *, so now &! =

 

++

Reposted explanation: http://blog.csdn.net/megaboy/archive/2005/10/17/505927.aspx

 

Repeat pointer

In this postscript, I will add three questions:

I. Questions about getting the address of the array name. C89 and c99 allow the array name to be the address, because the array conforms to the definition of an object, according

It is reasonable to get the address for the semantics of objects. However, the contradiction is that the array name is a symbolic address and a right value.

Conforms to the & operator syntax. The c89 and c99 Committees considered that the rationality of maintaining an object is more important and more appropriate than an operator.

Therefore, the address of the array name is allowed. However, the meaning of & A is not to take the address of an array name, but to take an array object

Therefore, the address value represented by & A is the same as the address value of A, and sizeof (& A) should be equal to sizeof (). C89 and c99

The point is as follows:

Some implementations have not allowed the & operator to be applied to an array or a function. (The construct was permitted in early versions of C, then later made optional .) the c89 Committee endorsed the construct since it is unambiguous, and since data into action is enhanced by allowing the important & operator to apply uniformly to any addressable entity.

II. For the assignment between level-2 const pointers, the author once gave an example in chapter 9 to show that the assignment between level-2 const pointers will

A constant is modified. Here is another reason. Taking const int * P1 and int * P2 as examples, two types are involved. First, p1

And P2 point to the object is int, this is compatible; P1 with const and P2 does not, this is also compatible, because it conforms to"

The left value of the value must contain all the qualified words of the right value. But for const int ** P1 and INT ** P2, the situation is different. Although

P1 still complies with the "Left value contains all right-value qualified words" rule, but the object that P1 points to is a pointer to the const object, while P2 points

The directed object is a pointer to a non-const object. The two types are incompatible, so p1 = P2 is invalid. This is an outdated reason

More often in early compilers, because it is often difficult for users to understand, so later compilers tend to allow such

Assign values.

Iii. Regarding the debate between P () and (* p) (), it was suggested that the form of P () may be dangerous because of an implicit statement in c89.

.

First, the form of P () recommended by c89 and c99 is a fact that has been finalized. The two standards are as follows:

Pointers to functions may be used either as (* PF) () or as PF (). the latter construct, not sanctioned in K & R, appears in some present versions of C, is unambiguous, invalidates no old code, and can be an important shorthand. the shorthand is useful for packages that present only one external name, which designates a structure full of pointers to objects and functions: member functions can be called as graphics. open (File) instead of (* graphics. open) (file ).
...........................
The c89 Committee saw no real harm in allowing these forms; outlawing forms like (* f) (), while still permitting * A for a [], simply seemed more trouble than it was worth.

Visible (* p) () is an old rule in the K & R era. c89 and c99 still support it only because they support the old code.

Second, these people think that when P () is used without declaring p, p is already declared because of the existence of this implicit statement.

In fact, c89 and c99 only regard P () as extern int P (); in this case. In syntax, P still

It is an undeclared identifier! It violates another rule: an identifier should be declared before it can be used. But it should not be like

It does not declare the identifier to generate an error. Otherwise, the implicit declaration is no different from the non-existent one. It should generate a warning

It indicates that P is undefined or the function prototype is not declared.

If there are any problems, some programmers may ignore this warning, which may cause some problems.

But this is the reason for the designer, not the standard. C99 simply abolished the implicit statement of c89 to prevent such risks.

The compiler generates an error. C99 says this:

The rule for implicit declaration of functions has been removed in c99.the effect is to guarantee the production of a diagnostic that will catch an additional category of programming errors.

The fourth point is written in November 8.

Fourth. Chapter 10 explains why the return value of a function cannot be an array. In this article, the author writes:

Int func (void) [5];

Func is a function that returns an array with five int elements. However, the function return value in C language cannot be an array. This is because if the function return value is an array, what receives the content of this array must also be an array, however, the array name in C language is a right value, which cannot be used as the left value to receive another array. Therefore, the return value of the function cannot be an array.

The preceding explanation is biased and the array name should not be used to explain this problem, because the array name is not a reference to the array object and there is no reference to the array object in C. Therefore, the Return Value of the function cannot be an array.

++

 

/* References:

Http://blog.csdn.net/perddy/archive/2008/04/22/2314985.aspx

Http://blog.csdn.net/megaboy/archive/2005/10/17/505927.aspx

*/

 

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.