Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand C # array and pointer

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the C # array and pointer". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to understand the C # array and pointer.

When the C # array and the C # pointer are the same

All array names as function arguments can always be converted to pointers through the compiler. In all other cases, the declaration of an array is an array, and the declaration of a pointer is a pointer, and the two should not be confused. However, when referenced in a statement or expression, the array can always be written in the form of a pointer, and the two are interchangeable. However, C # arrays and C # pointers are different when processed by the compiler and are represented differently at run time. For the compiler, an array is an address, and a pointer is the address of an address, and you should choose according to the situation.

Why is there confusion?

When people learn to program, they always put all the code in one function at first. As the level advances, they put the code in several functions. As the level continued to improve, they finally learned how to use several files to construct a program.

When are the C # array and C # pointers the same? The C # language standard states this as follows:

The array name in the rule 1 expression (different from the declaration) is treated by the compiler as a pointer to the elements of the array.

Rule 2 the subscript is always the same as the offset of the pointer.

Rule 3 in the declaration of function parameters, the array name is treated by the compiler as a pointer to the elements of the array.

Rule 1: "Array name in expression" is the pointer

Rules 1 and 2 are understood together that a reference to an array subscript can always be written as "a pointer to the starting address of the array plus an offset." (personally, it is more clear to express as "a pointer to the elements of the array plus an offset". )

References to arrays such as a [I] are always rewritten by the compiler in the form of * (axii) at compile time. The C # language standard requires the compiler to have this conceptual behavior. Therefore, both a [6] and 6 [a] are correct.

The compiler automatically adjusts the step size of the subscript value to the size of the array elements. This is why pointers always have type restrictions, and each pointer can only point to one type, because the compiler needs to know how many bytes should be taken when dereferencing the pointer and how many bytes should be taken for each subscript step.

Regular 2:C# language takes array subscript as the offset of pointer.

In fact, subscript range detection is not considered worthy of being added to the C # language. Array subscripts are defined on the basis of pointers, so the optimizer can often convert it into a more efficient pointer expression and generate the same machine instructions. The fundamental reason why the C # language rewrites array subscripts into pointer offsets is that pointers and offsets are the basic models used by the underlying hardware.

Why the C # language uses array parameters as pointers

The reason for converting array parameters passed to functions to pointers is for efficiency reasons, which is often a justification for violating software engineering practices. We tend to always define parameters as pointers because this is the form used internally by the compiler. The above describes the C # array and C # pointer.

Thank you for your reading, the above is the content of "how to understand the C # array and pointer". After the study of this article, I believe you have a deeper understanding of how to understand the C # array and pointer. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report