The first chapter analysis of the concept of arrays and pointers

Source: Internet
Author: User

Arrays and pointers are born of twins, and most are the journey from the beginning of the learning of the array to the pointer. In the course of learning, it is very natural to hear or see a variety of ideas about arrays and pointers, below I excerpt some of the various forums and articles often see the text:

"One-dimensional arrays are first-level pointers"

"Two-dimensional array is a level two pointer"

"The array name is a constant pointer"

"Array name is a pointer constant"

........................

These words look very familiar, right? There are a lot of similar words. Unfortunately, the text is wrong, and the array name is never a pointer! This conclusion may surprise you, but it is true. However, before discussing this problem, there are two questions to solve: what is a pointer? What is an array? This is the main content of this chapter, the question of whether the array name pointer is left in chapter two for discussion. See here, perhaps someone in the heart will whisper, so simple question still need to say? int *p, a[10]; Is it a pointer and an array? However, in the past discussions, I really found that many people are far from clear about these two concepts, which will hinder the understanding of the later content, so it is necessary to discuss the first.

What is a pointer? A universal understanding is that the pointer variable is understood to be a pointer, which is one-sided, the pointer variable is just a form of the pointer, but the pointer is not just a pointer variable. A pointer that contains two meanings: entity and type. The standard describes the pointer type as follows:

6.2.5 Types

A pointer type may is derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called "pointer to T". The construction of a pointer type from a referenced type is called ' ' pointer type derivation '.

Notice what the second sentence says: The pointer type describes an object whose value is a reference to a type entity. The wording used in the standard here is that the pointer type describes an object.

Let's take a look at the criteria for the & of the FETCH operator:

6.5.3 . 2 Address and indirection operators

Semantics

The unary & operator returns the address of its operand. If the operand has type ' type ', the result has a type ' pointer to type ' .... Otherwise, the result is a pointer to the object or function designated by its operand.

This clause specifies that the result of the,& operator is a pointer. But the problem is that the result of the,& expression is not an Object! Is the standard contradictory? Of course not, this means that the pointer entity has two forms of objects and non-objects.

We often say that the pointer variable is only the object shape of the pointer entity, but the object and non-object two form together, is the full meaning of the pointer, that is, whether or not an object, as long as a pointer-type entity, can be called pointers, in other words, the pointer is not necessarily an object, not necessarily a variable. In the latter case, when a temporary object of a pointer type needs to be produced, such as the return of a function or an intermediate result of an expression evaluation, it is not a variable because it is an unnamed temporary object.

In C + +, because of the introduction of OOP, an entity that is also known as a pointer is added: A class non-static member pointer, although also called a pointer, but it is not a general sense of the pointer. The C + + standard says this:

3.9.2 Compound Types

....... Except for pointers to static members, text referring to ' pointers ' does not ' apply to ' pointers to ' members ' ....

Next, it's time to talk about arrays. An array is an object whose object type is called an array type. But the author found that there is a strange phenomenon, some people do not have the array type of consciousness, but also some books do not have the array as a type to explain, perhaps the reason lies in it. Array types and pointer types are derived types, and the standard terms:

6.2.5 Types

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the Element type. Array types is characterized by their element type and by the number of the elements in the array. An array type was said to being derived from it element type, and if its element type was T, the array type is sometimes Calle D "Array of T". The construction of an array type from a element type is called "Array type derivation".

An array type describes a non-empty collection of objects, does not allow 0 elements, and we sometimes see a struct definition internally defining an array member of size 0, which is a non-standard form of a flexible array member, which is left in the eighth chapter. The syntax for the array type (note that it is not the declaration syntax of the array object) is element Type[interger constant], for example

int a[10];

The array type description of a is int[10].

The array object's identifier is a special identifier that has been handled implicitly by exception. The identifier of an integer object, the identifier of a floating-point number, and so on are identifiers, but there is a significant difference in the array name compared to it. The purpose of computer language is to translate human natural language into machine language that can be understood by computer, make it easier for human to use and manage all kinds of computer resources, easy-to-use is thought, abstract is method, language will abstract computer resources into a variety of language symbols and language rules, arrays, pointers, integers, Floating-point numbers and so on, these things are essentially different abstractions of memory operations. As an abstract method, it can be summed up as two implementations, one is that the name represents a finite space, its content is called a value, and the second is that the name is a reference to a finite space, while the length of the space is specified. The first method is commonly used in various computer languages, and is referred to as the conversion from Lvalue to rvalue in C + +. But the array is different from the general integer, floating-point object, it is a aggregation, it is not possible to think of a aggregation as a value, from a cluster value, in the C + + object model of the lack of rationality, it is meaningless. In most cases of expression evaluation, the first method does not fit into an array, and the second method converts the array name to a reference for a memory space.

Therefore, the array name is both generic and special compared to the general identifier. The general expression in its object nature is the same as the general identifier, in this case the array name, representing the array object, and because it conforms to the value-C + + model of the lvalue, it is an lvalue, but is not modifiable, the reason for non-modification is the same as the content described in the previous paragraph, It is meaningless to attempt to modify the whole aggregation by a name, while the particularity is reflected in the expression's calculation, that is, the array and pointer conversion clauses described in the C/s standard, in which the array name is not converted to the value of the object, but rather a symbolic address.

Now let's look at how the standard specifies the conversion of arrays to pointers:

Content of C89/90:

6.2.2 . 1 Lvalues and function designators

Except when it was the operand of the sizeof operator or the unary & operator, or was a character string literal used to Initialize an array of character type. Or is a wide string literal used to initialize an array with element type compatible with WCHAR-T, an lvalue that has type The "Array of type" is converted to an expression of the have type "pointer to type" that points to the initial element of the A Rray object and is a lvalue.

Content of C99:

6.3.2 . 1 Lvalues, arrays, and function designators

Except when it's the operand of the sizeof operator or the unary & operator, or is a string literal used to Initializ E an array, an expression of this has type ' array of type ' is converted to a expression with type ' pointer to type ' that poi NTS to the initial element of the array object and are not a lvalue. If the Array object has a register storage class, the behavior is undefined.

The result of an array type-to-pointer-type conversion is a pointer to the first element of the array of type pointer to type, and is converted from an lvalue to an rvalue value. After conversion, the array name no longer represents the array object, but rather a symbolic address that represents the first address of the array (this sentence should be the symbolic address of the first element of the group, April 2011), and is not an object. In particular, the array-to-pointer conversion rule applies only to expressions, in which case the array name is the result of the transformation, which represents the first address of the array (which should be the address of the first element of the group, April 2011), and when the array is known as an identifier for the object definition, the initializer and as sizeof, & Operand, it represents the array object itself, not the address.

This conversion brings a benefit, which is advantageous for pointer operations inside the array. We can use a + 1 this refined form to represent the address of a[1], without &a[1] This ugly code, in fact, &a[1] is a code redundancy, is a waste of code, because &a[1] equivalent to &* (A + 1),& and * Since the effect is reversed, it is actually a + 1, so why don't we just use a + 1? Aside from the readability of human reading habits, &a[1] is rubbish.

However, on the other hand, this exception to the general identifier Lvalue conversion greatly increases the complexity of the array and pointers, puzzles the beginner countless days and nights of the thought of the storm has opened the curtain!

Of the two versions of the conversion clause, one thing to note is that two versions have different descriptions of expressions with array types.

C89/90 regulations:

An lvalue. Have type "array of type" is ...

But C99 the rule:

An expression, which has the type "array of Tye" is ....

C99 removed the words of lvalue, why? We know that the array name is a non-modifiable lvalue, but in fact there is an array of rvalue values. In C, an lvalue is an expression with an object type or a non-void incomplete type, and C's Lvalue expression excludes function and function calls, and C + + because the reference type is added, the function call that returns the reference is also an lvalue expression, that is, the function call returned by a non-reference is an rvalue. What happens if the function non-reference returns contain an array? Consider the following code:

#include <stdio.h>

struct Test

{

int a[10];

};

struct Test fun (struct test*);

int main (void)

{

struct Test T;

int *p = Fun (&t). A; /* A */

int (*Q) [Ten] = &fun (&t). A; /* B */

printf ("%d", sizeof (Fun (&t). a)); /* c*/

return 0;

}

struct test fun (struct test *t)

{

return *t;

}

In this example, the return value of fun (&t) is a right value, fun (&t). A is an rvalue array, an rvalue expression, but a itself is an lvalue expression, pay attention to this difference. In c89/90, since the array-to-pointer conversion is required for an array of lvalue values, the fun (&t) in a. A cannot be converted from an array type to a pointer type in an expression, fun (&t) in a. A is illegal, But C99 no longer limits the lvalue expression in the above clause, that is, the conversion no longer distinguishes between an lvalue or an rvalue array, so it is legal, and C is fun (&t). A is the operand of the sizeof operator, in which case fun (&t). A does not perform array-to-pointer conversions, So C is all legal in all C + + standards; b, it still looks a little weird at first.,& operator is not already an exception to exclude array and pointer conversion? Why is it still illegal? In fact, B violates another rule.,& 's operand requirement is lvalue, while fun (&t). A is the right value. C + + inherits the view of C99 and also allows the conversion of rvalue arrays, whose terms are very simple:

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can is converted to an rvalue of type "pointer To T. " The result is a pointer to the first element of the array.

The conversion of an array type to a pointer type is a very important conversion rule for C + + three, along with the conversion of the lvalue to the right value, the conversion of the function type to the pointer type. C + + because of the need for overload resolution, the three rules are conceptualized, collectively referred to as lvalue conversion, but C because there is no such need, only put forward the rules. A symbol is a high-level abstraction of a computer by a language, but a computer does not recognize a symbol, it only knows the value, so a symbol must be numerically evaluated to participate in the expression calculation, and three conversion rules exist for that purpose.

See here, probably some beginners have been above those left value right value, object non-object get confused. Indeed, the complexity of arrays and pointers is daunting, not overnight, and takes a longer time to digest. So the author will be an array and pointer called an art, yes, it is art!

The first chapter analysis of the concept of arrays and pointers

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.