Array and pointer-memory Angle

Source: Internet
Author: User
Tags array definition

Previous semantic trap-array and pointer

After talking about the differences between arrays and pointers, we mainly differentiate the access methods of arrays and pointers in the memory. This blog post analyzes the relationship between arrays and pointers from a deeper perspective.

If you are also interested in the underlying layer, I will help you with this article,

When the Array and Pointer are the same (When an Array Is a Pointer)

In practice, they are more interchangeable than not. First, review the statements and definitions)

The statement itself can be further divided into three situations:

1) external array declaration (external array)

2) array definition (it is a special situation declared, it allocates memory space, and may provide an initial value)

3) function parameter Declaration

All declarations used as function parameters are always converted to pointers (pointing to the first element of the array) during compilation. In other cases, the declarations are arrays and pointers.

The following figure shows their relationship:

 

Summary:

When an array is converted into a pointer during compilation, the array and pointer can be exchanged. For example, when declared as a function parameter, fun (int a []) and fun (int *) is equivalent. Because fun (int a []) during compilation

The array in will be converted to the pointer form, which is the same as the effect of fun (int *.

If the array is not processed as a pointer at the time of compilation, it cannot be declared as equivalent. In this case, arrays and pointers are different, and the representation at runtime is different, and different code may be generated.

For the compiler, an array is an address, and a pointer is the address.

The C language standards are described as follows:

1) An array name in an expression (in contrast with a declaration) is treated by the compiler as a pointer to the first element of the array
(Paraphrase, ansi c Standard, paragraph 6.2.2.1 ).

2) A subscript is always equivalent to an offset from a pointer (paraphrase,

Ansi c Standard, paragraph 6.3.2.1 ).

3) An array name in the declaration of a function parameter is treated

The compiler as a pointer to the first element of the array (paraphrase, ANSI
C Standard, paragraph 6.7.1 ).

That is:

1) The array name in the expression (different from the declaration) is used by the compiler as a pointer to the first element of the array.

2) The subscript is always the same as the offset.

3) in the declaration of function parameters, the array name is treated as a pointer to the first element of the array by the compiler.

I think it is necessary to explain the "expression" in the above text.

Int arry [10] = {,,,,,};

Int a = arry [2];

In the second sentence, int a = arry [2]; is the name of the array in the so-called expression. At this time, the compiler will regard the array name arry as a pointer to the first element of the array, that is, the address of arry [0 ].

The subscript is always the same as the offset, that is, the subscript 2 and arry [2] In arry [2] are the same as the offset of the first element in memory. In this way, the array subscript can be used to obtain

An element in the corresponding array. (Of course, The step size should be considered in the memory)

With the above analysis, it is easy to understand the following

If declared: int a [10], * p, I = 2

Then we can access a [I] through any of the following methods (each column is a group of three)

P = a; p = a + I;
P [I]; * (p + I); * p;

In expressions, arrays and pointers can be exchanged because they are in the pointer form in the compiler and can perform subscript removal.

Array and pointer Traversal

For better understanding, the following example illustrates the relationship and difference between arrays and pointers. (Difficult to understand ~ Oh)

We will discuss array and pointer traversal from the perspective of memory access.

Array traversal:

For (I = 0; I <10; I ++)

A [I] = 0

Traversal process:

1) put the left value of a into Register R1 (that is, store the physical address of a, that is, the first address of the array, to R1) outside the loop.

2) put the left value of I into R2, the same as above, that is, the physical address of I into R2 can be moved out of the loop

3) put the right value of [R2] into R3, that is, put the size of variable I into R3 (this is a bit of a compilation flavor)

4) if needed, adjust the step size of R3 and put the value of R1 + R3 into R4. Explanation: R1 is the first address of the array and R3 is the offset. Therefore, R4 is the address of the current operand.

5) Put 0 into [R4]

Note: The above R1-R4 to see what is done is a register, the symbol [n] indicates: memory address is n Value

"Can be moved out of the loop" indicates that it will not change throughout the process, such as the first address of the array, variable I address

The concepts of left and right values are described in the blog above.

Pointer traversal:

P =

For (I = 0; I <10; I ++)

* P ++ = 0

Traversal process:

1) The object size indicated by p can be moved to R5 outside the loop.

2) put the left value p into R1 and move it out of the loop

3) put [R0] into R1

4) 0 to [R1]

5) The R5 + R1 result is saved to R1

6) R1 is stored in [R0]

In fact, these two access methods can also be seen as a deeper understanding of the access methods in the previous blog.

To operate a variable, you need to get the address of the variable. The difference and connection between the array and the pointer are obtained. This is not the case. If you are interested, refer to the previous blog.

Http://www.cnblogs.com/yanlingyin/archive/2011/11/29/2268391.html

Why does C regard array parameters as pointers?

Consider the array as a pointer for efficiency. In C, all data parameters that are not in the form of arrays are passed in the form of values, that is, when a function is called, the real parameters

Copy one copy to the called function, that is, the function operation is to copy the real parameters rather than the real parameters themselves. (If the parameter value is changed in the function when the value is passed, the actual real parameter is not

Because the function operation in value passing only copies the real parameters, not the real parameters themselves)

For arrays, if you copy the entire array every time you execute a function, it will take a lot of time and space overhead. For all arrays, C is used to tell the first address of the Function Array.

Array process operations.

A friend who understands C ++ should be able to better understand this. In C ++, parameter passing is divided into value passing and reference passing, if you are interested, you can view the information on your own. This is not the end of this article.

An exchange Summary of Arrays and Pointers: Arrays and Pointers Interchangeability Summary

1. An array access a [I] is always "rewritten" or interpreted by the compiler as a pointer access * (a + I );
2. Pointers are always just pointers; they are never rewritten to arrays. You can apply
Subscript to a pointer; you typically do this when the pointer is a function argument,
And you know that you will be passing an array in.
3. An array declaration in the specific context (only) of a function parameter can equally
Be written as a pointer. An array that is a function argument (I. e., in a call to
Function) is always changed, by the compiler, to a pointer to the start of the array.
4. Therefore, you have the choice for defining a function parameter which is an array,
Either as an array or as a pointer. Whichever way you define it, you actually get
Pointer inside the function.
5. In all other cases, definitions shocould match declarations. If you defined it as an array,
Your extern declaration shocould be an array. And likewise for a pointer.

1) For access arrays in the form of a [I], it is usually interpreted as the pointer form * (a + I), that is, the "expression" mentioned above.

2) A pointer is a pointer. If a pointer is converted to an array, you can access the pointer in the form of a lower mark. However, when a pointer is used as a function parameter, an array is passed in.

3) in the declaration of function parameters, Arrays can be seen as pointers (and only in this case)

4) When an array is defined as a function parameter, it can be defined as an array or a pointer.

5) in all other cases, the declaration and definition must match. If an array is defined, it must also be declared as an array in other files. The same is true for pointers.

 

Reference: expert c programming

 

If reprint please indicate the source: http://www.cnblogs.com/yanlingyin/

A fish @ blog

2011-12-6

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.