📜  如何在C#中使用指针访问结构元素

📅  最后修改于: 2021-05-29 16:52:03             🧑  作者: Mango

与C / C++不同,C#中的结构可以具有方法,字段,索引器,运算符方法,属性或事件等成员。成员可以具有公共,私有和内部访问说明符。
指针是存储相同类型变量的地址的变量,即,int指针可以存储整数的地址,char指针可以存储char的地址,并且对于所有其他基本或用户定义的数据类型,其指针都类似。

您可以通过以下方式使用结构类型为指针的结构成员:

1)使用箭头运算符:如果结构的成员是公共的,则可以使用箭头运算符(->)直接访问它们。如果它们是私有的,则可以定义用于访问值的方法,并使用指针来访问方法。箭头运算符可用于访问结构变量以及方法。

句法:

PointerName->memberName;

例子:

// C# Program to show the use of 
// pointers to access struct members
using System;
  
namespace GFG {
  
// Defining a struct Student
struct Student
{
    // With members
    // roll number and marks
    public int rno;
    public double marks;
  
    // Constructor to initialize values
    public Student(int r, double m)
    {
        rno = r;
        marks = m;
    }
}; // end of struct Student
  
class Program {
  
    // Main Method
    static void Main(string[] args)
    {
        // unsafe so as to use pointers
        unsafe
        {
            // Declaring two Student Variables
            Student S1 = new Student(1, 95.0);
            Student S2 = new Student(2, 79.5);
  
            // Declaring two Student pointers
            // and initializing them with addresses
            // of S1 and S2
            Student* S1_ptr = &S1;
            Student* S2_ptr = &S2;
  
            // Displaying details of Student using pointers
            // Using  the arrow ( -> ) operator
            Console.WriteLine("Details of Student 1");
  
            Console.WriteLine("Roll Number: {0} Marks: {1}", 
                            S1_ptr -> rno, S1_ptr -> marks);
  
            Console.WriteLine("Details of Student 2");
            Console.WriteLine("Roll Number: {0} Marks: {1}",
                            S2_ptr -> rno, S2_ptr -> marks);
  
        } // end unsafe
  
    } // end main
  
} // end class
  
}

输出:

2)使用解引用运算符:您还可以使用指针上的解引用运算符来访问结构元素,该运算符使用星号解引用指针,然后使用点运算符指定结构元素。

句法:

(*PointerName).MemberName;

例子:

// C# Program to illustrate the use 
// of dereferencing operator
using System;
  
namespace GFG {
  
// Defining struct Employee
struct Employee{
  
    // Elements Eid and Salary
    // With properties get and set
    public int Eid 
    {     
        get;
        set;
    }
  
public double Salary
{
    get;
    set;
}
}
; // Employee ends
  
class Program {
  
    // Main Method
    static void Main(string[] args)
    {
        // unsafe so as to use pointers
        unsafe
        {
            // Declaring a variable of type Employee
            Employee E1;
  
            // Declaring pointer of type Employee
            // initialized to point to E1
            Employee* ptr = &E1;
  
            // Accessing struct elements using
            // dereferencing operator
            // calls the set accessor
            (*ptr).Eid = 1101;
            (*ptr).Salary = 1000.00;
  
            Console.WriteLine("Details of Employee");
            // calls the get accessor
            Console.WriteLine("Employee Id: {0}\nSalary: {1}",
                                   (*ptr).Eid, (*ptr).Salary);
  
        } // end unsafe
  
    } // end Main
  
} // end class
  
}

输出:

注意:要在Visual Studio(2012)上编译不安全代码,请转到项目–> ProjectName属性–>生成–>选中“允许不安全代码”框。