个性化阅读
专注于IT技术分析

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

点击下载

与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
  
}

输出如下:

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

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
  
}

输出如下:

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

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


赞(1)
未经允许不得转载:srcmini » 如何在C#中使用指针访问结构体元素

评论 抢沙发

评论前必须登录!