【C#學習筆記】指標使用

Dsp Tian發表於2017-08-27
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            unsafe
            {
                int* pa = &a;
                Console.WriteLine(*pa);
            }
/************************************************************************/
            int[] b = new int[10];
            unsafe
            {         
                fixed (int* pb = b)
                {
                    for (int i = 0; i < 10;i++ )
                    {
                        *(pb+i) = i;
                    }
                }
            }
            foreach (int i in b)
                Console.Write(i);
            Console.WriteLine();
/************************************************************************/
            unsafe
            {
                int* s = stackalloc int[5];
                for (int i = 0; i < 5;i++ )
                {
                    *s=i;
                    s++;
                }
                s = s - 5;
                for (int i = 0; i < 5;i++ )
                {
                    Console.Write(*(s+i));
                }
            }

            Console.Read();
        }
    }
}

 

相關文章