9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
0-A, 1-B, 2-C, 3-D, 4-E, |
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
| csc /unsafe stackalloc.cs |
/****************************************************/
/*★ stackalloc で配列のポインタを使う 前田 稔 ★*/
/****************************************************/
using System;
class UnsafeTest
{
static void Main()
{
unsafe
{
int* x = stackalloc int[10]; // 配列をスタック上に確保
int* pt;
// 配列 x に 0~9 を代入
for(int i=0; i<10; ++i) x[i] = i;
// 配列を逆に出力
Console.Write('\n');
pt = x+9;
for(int i=0; i<10; ++i,pt--) Console.Write("{0}, ", *pt);
Console.Write('\n');
}
}
}
|
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
/*******************************************************/
/*★ fixed で領域を固定してポインタを使う 前田 稔 ★*/
/*******************************************************/
using System;
class UnsafeTest
{
static void Main()
{
unsafe
{
char[] x = new char[5];
for(int i=0; i<5; ++i) x[i] = (char)('A'+i);
fixed(char* pt= x)
{
// 配列 x を表示
for(int i=0; i<5; i++) Console.Write("{0}-{1}, ", i,*(pt+i));
}
}
}
}
|
0-A, 1-B, 2-C, 3-D, 4-E, |
![]()