C# 二维数组可否使用指针

2025-10-21 08:28:07

1、C#可以定义为指针的类型有:

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool,

 

struct(结构体),结构体中只能包括非托管类型。

C#指针操作符:

* 取值运算符

& 取址运算符

-> 通过指针处理结构体中的数据(获取或赋值)

++与– 指针增、减操作

fixed 用户暂时固定托管代码中引用类型的位置。

Stackallc 分配内存

2、C#可以定义为指针的类型有:

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool,

 

struct(结构体),结构体中只能包括非托管类型。

3、C#指针的定义:

定义指针说明

int* p整形指针

int** p指向整形指针的指针

char* c指向字符的指针

int*[] arr整形一维数组指针

4、C#定义一维二维数组的方式如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //一维数组定义与初始化

            int[] one1 = new int[] {3,2,1 };//第一种方式

            int[] one2 = { 3, 2, 1 };      //第二种方式

            int[] one3;                    //第三种方式

            one3=new int[]{3,2,1};

          

   //二维数组定义与初始化

            //不规则二维数组

            int[][] array = new int[2][];

            array[0] = new int[3];

            array[0][1] = 11;

            array[0][2] = 12;

            array[1] = new int[] { 1, 2, 3, 4,5 };

            //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }};

          

            Console.WriteLine( "不规则二维数组:  ");

            for (int i = 0; i < array.Length; i++)

            {

                foreach (int j in array[i])

                {

                    Console.Write(j+"   ");

                }

                Console.WriteLine();

            }

            //固定长度的矩阵数组

            int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}};

            Console.WriteLine("规则二维数组输出方法一:  ");

            for (int ii = 0; ii < 2; ii++)                        //输出方法一

            {

                for (int j = 0; j < 5; j++)

                {

                    Console.Write(arrray1[ii,j] + "   ");

                }

                Console.WriteLine();

            }

            Console.WriteLine("规则二维数组输出方法二:  ");

            foreach (int j in arrray1)//arrray.length=10;      //输出方法二

            {

                Console.Write(j + "   ");

            }

            Console.WriteLine();

            Console.ReadLine();

        }

    }

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢