欢迎访问我的博客 https://javascript.shop

重新复习基础草稿:不安全代码即指针的简单使用

原文发布时间为:2008-12-06 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Collections.Generic;
using System.Text;


//"不安全代码只会在使用 /unsafe 编译的情况下出现"的解决方法
//右击右上角窗口中的项目(不是解决方案,而是它下面那个)——属性——生成——允许不安全代码 前面打钩
//这样就可以了。。。
namespace fanxing1
{
unsafe class Class3
    {
        static void Main()
        {
            int number = 1024;

            unsafe
            {
                // Convert to byte:
                byte* p = (byte*)&number;

                System.Console.Write("The 4 bytes of the integer:");

                // Display the 4 bytes of the int variable:
                for (int i = 0; i < sizeof(int); ++i)
                {
                    System.Console.Write(" {0:X2}", *p);
                    // Increment the pointer:
                    p++;
                }
                System.Console.WriteLine();
                System.Console.WriteLine("The value of the integer: {0}", number);
            }

            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);

            int num=8;

        unsafe
        {
            // Assign the address of number to a pointer:
            int* p = &num;

            System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p);
            // Commenting the following statement will remove the
            // initialization of number.
            *p = 0xffff;

            // Print the value of *p:
            System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p);

            // Print the address stored in p:
            System.Console.WriteLine("The address stored in p: {0}", p->ToString());
        }

        // Print the value of the variable number:
        System.Console.WriteLine("Value of the variable number: {0:X}", num);
           
        CoOrds home;
        unsafe
        {
            CoOrds* p = &home;
            p->x = 25;
            p->y = 12;
            System.Console.WriteLine("The coordinates are: x={0}, y={1}", p->x, p->y );
        }

        char* charPointer = stackalloc char[123];

        for (int i = 65; i < 123; i++)
        {
            charPointer[i] = (char)i;
        }

        // Print uppercase letters:
        System.Console.WriteLine("Uppercase letters:");
        for (int i = 65; i < 91; i++)
        {
            System.Console.Write(charPointer[i]);
        }
        System.Console.WriteLine();

        // Print lowercase letters:
        System.Console.WriteLine("Lowercase letters:");
        for (int i = 97; i < 123; i++)
        {
            //System.Console.Write(charPointer[i]);
            System.Console.Write(*(charPointer+i));
        }

        int[] numbers = { 0, 1, 2, 3, 4 };

        // Assign the array address to the pointer:
           
        fixed(int* p1 = numbers)
        {
            // Step through the array elements:
            for (int* p2 = p1; p2 < p1 + numbers.Length; p2++)
            {
                System.Console.WriteLine("Value:{0} @ Address:{1}", *p2, (long)p2);
            }
        }

        int* memory = stackalloc int[30];
        long* difference;
        int* p4 = &memory[4];
        int* p5 = &memory[10];
        difference = (long*)(p5 - p4);
        System.Console.WriteLine("The difference is: {0}", (long)difference);
        int x0 = 234;
        int y0 = 236;
        int* p10 = &x0;
        int* p20 = &y0;
        System.Console.WriteLine(p10 < p20);
        System.Console.WriteLine(p20 < p10);
            Console.ReadLine();
        }
    }

    struct CoOrds
    {
        public int x;
        public int y;
    }

}

posted @ 2017-07-11 00:37  孑孓子  阅读(124)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop