The second chapter is the array name a pointer constant?

Source: Internet
Author: User
Tags array definition

The array name is a pointer constant, which is derived from the equivalence of the result of the array name to the pointer in the expression evaluation. For example, the following code:

int a[10], *p = A, *q;

Q = a + 1;

Q = p + 1;

In effect, a + 1 and P + 1 are the same, it is easy to give a is the illusion of P, but, this is just an illusion. Since pointer constants contain pointers and constants class two concepts, we can discuss this problem in two separate parts.

First, is the array name a pointer?

In the book "C and Pointers", the author uses a well-known example to illustrate the difference between the array name and the pointer. Define in one file: int a[10]; then declare in another file: extern int *a; The author does not repeat the principle here, the author of the book is trying to explain from the bottom operation of the array name and pointer different points, but I think this example has some shortcomings, a in the expression will be converted to a non-object symbolic address, and pointer A is an object, with a non-object to compare with an object, there is "stealing" Suspect, this example simply illustrates the non-object nature of the array name and only proves that the object differs from the non-object entity in the underlying operation, in fact, as described in the previous chapter, pointers also have non-object forms. The author argues that it is not necessary to spend so much of your breath from the underlying point of view, just literal semantics can overturn the idea that the array name is a pointer.

First of all, the array type and pointer type are two different derived types, and the array name and pointer are two different types of entities, and it is absurd to say that the entity of the array type is "yes" and another type of entity.

Second, the effect of a + 1 is equivalent to P + 1, because A is an array-to-pointer implicit conversion, which is a process of conversion, converted to rather than is a. If there are two identical things, how can there be a process of conversion? When a is placed in a + 1 expression, A has been converted from an array name to a pointer, a is a pointer instead of the array name to participate in the operation;

Thirdly, a + 1 and P + 1 are equivalent relationships, not equivalence relations. Equivalence is the different manifestation of the same thing, and equivalence is the same effect of different things. The equivalent relationship is misinterpreted by the fact that the array name is a pointer.

Therefore, the array name is not a pointer and is never, but under certain conditions, the array name can be converted to a pointer.

Second, is the array name a constant?

It is strange to see this sentence, after the array definition can not be changed, the array name is not a constant? In an expression, the array name can indeed be converted to an immutable symbolic address, but in c the immutable entity is not necessarily a constant! Also, C + + has constants and constant expressions, constants and constant expressions are two different entities, but constant expressions can be used as constants. Constants in C/s + + are different, but do not include array or array names, and the array name is not necessarily a constant expression.

Compile the following code in the compiler for C90, noting that it cannot be C99 and C + +, because C99 and C + + no longer specify that the initializer for an array must be a constant expression and will not see the effect:

int main (void)

{

static int a[10], b[10];

int c[10], d[10];

int* e[] = {A, b}; /* A */

int* f[] = {c, d}; /* B */

return 0;

}

b Why not compile? is because the automatic array name is not a constant expression. In C, constant expressions must be compile-time, and entities that are unchanged at run time are not constant expressions, see the standard excerpt:

6.6 Constant Expressions

A constant expression can be evaluated during translation rather than runtime, and accordingly could be used on any place th At a constant.

C and D are automatic arrays, and the first address is not known at compile time, because such objects do not exist at compile time; A and b are static arrays, static objects exist from the beginning of the program, so the first addresses of a and b are known at compile time, and they all belong to address constant expressions in constant expressions.

Therefore, the array name in C + + is not a constant. The array name in C, whether the constant expression depends on its storage continuity, the global array, the static array names are constant expressions, and the automatic array name is not. In C + +, the array name of C + + is a constant expression because the constant expression is no longer required to be a compile-time.

Second chapter is the array name a pointer constant?

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.