码迷,mamicode.com
首页 > 其他好文 > 详细

C# 指针_铭轩同学原创

时间:2014-07-23 15:01:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   数据   

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1.指针的定义
            //      1.1指针是专门处理地址的数据类型
            //      1.2指针是某个内存单元的首地址

            // 2.C#指针的和C,C++的区别(这里我们需要知道两个关键字 unsafe 和 fixed)
            //      2.1指针在c#中是不提倡使用的,有关指针的操作被认为是不安全的(unsafe)。因此运行这段代码之前,先要改一个地方,否则编译不过无法运行。
            //         修改方法:
            //                  在右侧的solution Explorer中找到你的项目,在项目图标(绿色)上点右键,选最后一项properties,然后在Build标签页里把Allow unsafe code勾选上。之后这段代码就可以运行了,你会看到,上面这段代码可以像C语言那样用指针操纵数组。但前提是必须有fixed (int* p = array),它的意思是让p固定指向数组array,不允许改动。因为C#的自动垃圾回收机制会允许已经分配的内存在运行时进行位置调整,如果那样,p可能一开始指的是array,但后来array的位置被调整到别的位置后,p指向的就不是array了。所以要加一个fixed关键字,把它定在那里一动不动,之后的操作才有保障。


            //**********************下面直接看代码:*********************

            //1.1 int类型指针(值类型)
            //C#操作指针的时候必须放在unsafe代码块下
            unsafe
            {
                //声明一个int变量 值为10;
                int i = 10;
                //声明一个指针变量
                int* intp;
                //将指针指向变量i
                intp = &i;

                Console.WriteLine("变量I的值为:{0}", i);
                Console.WriteLine("变量I的内存地址为:{0:X}", (int)intp);
                Console.WriteLine("指针intp的内存地址为:{0:X}", (int)&intp);
                Console.WriteLine("指针intp的值为:{0}", *intp);
            }

            //1.2 string类型(引用类型)
            unsafe
            {
                //声明一个字符串为 hello world!(string类型就是一个char的数组)
                string _str = "hello world!";
                //上文已经说过,C#中因为有gc的原因会把放在托管椎中的内存进行位置调整fixed的作用就是让其固定.
                fixed (char* strp = _str)
                {
                    Console.WriteLine("变量_str的值为:{0}", _str);
                    Console.WriteLine("变量_str的地址为:{0}", (int)strp);
                    Console.WriteLine("指针strp的值为{0}", *strp);

                    //提示错误, 无法取得只读局部变量的地址 , 因为strp是只读的.编译器不让strp改变改指针.所以编译不通过
                    //Console.WriteLine("指针strp的地址为:{0}", (int)&strp);

                    //循环输出字符串
                    char* chp = strp;
                    for (int i = 0;
                         chp[i] != \0;        //字符串结束符号是 ‘\0‘
                         i++)
                    {
                        Console.Write(chp[i]);
                    }
                    Console.WriteLine();
                    Console.WriteLine("指针strp的地址:{0:x}", (int)chp);
                    Console.WriteLine("指针chp的地址:{0:x}", (int)&chp);
                }
            }

            //1.3数组
            unsafe
            {
                //声明一个数组
                int[] arrInt = { 1, 2, 3, 4, 5, 6, 7, 87, 8 };
                fixed (int* ip = arrInt)
                {

                    //循环输出数组
                    int* p = ip;
                    for (int i = 0; i < arrInt.Length; i++)
                    {
                        Console.Write(p[i] + " ");
                    }
                }
            }

            Console.ReadKey();
        }
    }
}
输出结果:
bubuko.com,布布扣

C# 指针_铭轩同学原创,布布扣,bubuko.com

C# 指针_铭轩同学原创

标签:style   blog   http   color   使用   数据   

原文地址:http://www.cnblogs.com/mingxuantongxue/p/3862746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!