共用方式為


HOW TO:取得指標變數值 (C# 程式設計手冊)

更新:2007 年 11 月

使用指標間接取值運算子,以取得位於指標所指之位置的變數。此運算式的格式如下,其中 p 為指標型別 (Pointer Type):

*p;

除了指標型別以外,您無法在任何其他型別的運算式上使用一元間接取值運算子。也無法將其套用至 void 指標。

當您將間接取值運算子套用至 null 指標時,結果會視實作而定。

範例

在下列範例中,會使用不同型別的指標存取型別 char 的變數。請注意,由於配置至變數的實體位置可能改變,因此 theChar 的位址在每次執行時都會不同。

// compile with: /unsafe
unsafe class TestClass
{
    static void Main()
    {
        char theChar = 'Z';
        char* pChar = &theChar;
        void* pVoid = pChar;
        int* pInt = (int*)pVoid;

        System.Console.WriteLine("Value of theChar = {0}", theChar);
        System.Console.WriteLine("Address of theChar = {0:X2}",(int)pChar);
        System.Console.WriteLine("Value of pChar = {0}", *pChar);
        System.Console.WriteLine("Value of pInt = {0}", *pInt);
    }
}
Value of theChar = Z 
Address of theChar = 12F718
Value of pChar = Z 
Value of pInt = 90 

請參閱

概念

C# 程式設計手冊

參考

指標運算式 (C# 程式設計手冊)

指標型別 (C# 程式設計手冊)

unsafe (C# 參考)

fixed 陳述式 (C# 參考)

stackalloc (C# 參考)

其他資源

型別 (C# 參考)