📄 25.2.htm
字号:
using System;
class Test
{
static int value = 20;
unsafe static void F(out int* pi1, ref int* pi2) {
int i = 10;
pi1 = &i;
fixed (int* pj = &value) {
// ...
pi2 = pj;
}
}
static void Main() {
int i = 10;
unsafe {
int* px1;
int* px2 = &i;
F(out px1, ref px2);
Console.WriteLine("*px1 = {0}, *px2 = {1}",
*px1, *px2); // undefined behavior
}
}
}
</pre>end note]</span> </span><span class="locator">
Paragraph 12</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P12S1"></a>A method can return a value of some type, and that type can be a pointer.</span> <span class="example">[Example: For example, when given a pointer to a contiguous sequence of ints, that sequence's element count, and some other <span class="keyword">int</span> value, the following method returns the address of that value in that sequence, if a match occurs; otherwise it returns null: <pre class="code-example">
unsafe static int* Find(int* pi, int size, int value) {
for (int i = 0; i < size; ++i) {
if (*pi == value) {
return pi;
}
++pi;
}
return null;
}
</pre>end example]</span> </span><span class="locator">
Paragraph 13</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P13S1"></a>In an unsafe context, several constructs are available for operating on pointers: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P13S2"></a> The * operator may be used to perform pointer indirection (<a href="25.5.1.htm">§25.5.1</a>).</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P13S3"></a> The -> operator may be used to access a member of a struct through a pointer (<a href="25.5.2.htm">§25.5.2</a>).</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P13S4"></a> The [] operator may be used to index a pointer (<a href="25.5.3.htm">§25.5.3</a>).</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P13S5"></a> The & operator may be used to obtain the address of a variable (<a href="25.5.4.htm">§25.5.4</a>).</span> </li><li><span class="sentence"><span class="sentence-number">6</span> <a name="P13S6"></a> The ++ and --operators may be used to increment and decrement pointers (<a href="25.5.5.htm">§25.5.5</a>).</span> </li><li><span class="sentence"><span class="sentence-number">7</span> <a name="P13S7"></a> The + and -operators may be used to perform pointer arithmetic (<a href="25.5.6.htm">§25.5.6</a>).</span> </li><li><span class="sentence"><span class="sentence-number">8</span> <a name="P13S8"></a> The ==, !=, <, >, <=, and => operators may be used to compare pointers (<a href="25.5.7.htm">§25.5.7</a>).</span> </li><li><span class="sentence"><span class="sentence-number">9</span> <a name="P13S9"></a> The stackalloc operator may be used to allocate memory from the call stack (<a href="25.7.htm">§25.7</a>).</span> </li><li><span class="sentence"><span class="sentence-number">10</span> <a name="P13S10"></a> The fixed statement may be used to temporarily fix a variable so its address can be obtained (<a href="25.6.htm">§25.6</a>).</span> </li></ul></span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -