using System;
struct Coordinate
{
public int x;
public int y;
public Coordinate(int x, int y)
{
this.x = x;
this.y = y;
}
} // end of struct
class Program {
static void Main(string[] args)
{
// unsafe so as to use pointers
unsafe
{
Coordinate C1 = new Coordinate(1, 2);
Coordinate C2 = new Coordinate(3, 4);
Coordinate* C1_ptr = &C1;
Coordinate* C2_ptr = &C2;
Console.WriteLine("Details of Coordinate 1");
Console.WriteLine("Coordinate 1 X = {0} , Y = {1}",
C1_ptr -> x, C1_ptr -> y);
Console.WriteLine("Details of Coordinate 2");
Console.WriteLine("Coordinate 2 X = {0} , Y = {1}",
C2_ptr -> x, C2_ptr -> y);
} // end unsafe
} // end main
} // end class
Details of Coordinate 1
Coordinate 1 X = 1 , Y = 2
Details of Coordinate 2
Coordinate 2 X = 3 , Y = 4
using System;
struct Coordinate
{
public int x;
public int y;
public Coordinate(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Program
{
static void Main(string[] args)
{
Coordinate C1 = new Coordinate(1, 2);
Coordinate C2 = new Coordinate(3, 4);
Console.WriteLine("Details of Coordinate 1");
Console.WriteLine("Coordinate 1 X = {0} , Y = {1}", C1.x, C1.y);
Console.WriteLine("Details of Coordinate 2");
Console.WriteLine("Coordinate 2 X = {0} , Y = {1}", C2.x, C2.y);
}
}
2 ) การใช้ Dereferencing operator
Syntax:
(*PointerName).MemberName;
Example:
using System;
struct Coordinate
{
public int x;
public int y;
} // end of struct
class Program {
static void Main(string[] args)
{
// unsafe so as to use pointers
unsafe
{
Coordinate C;
Coordinate* ptr = &C;
(*ptr).x = 10;
(*ptr).y = 20;
Console.WriteLine("Details of Coordinate");
// calls the get accessor
Console.WriteLine("Coordinate x:{0},y:{1}",
(*ptr).x, (*ptr).y);
} // end unsafe
} // end main
} // end class
Details of Coordinate
Coordinate x:10,y:20
คำอธิบายตัวอย่าง : ใน code นี้จะมีการสร้าง struct Coordinate ขึ้นมาโดยมีฟิลด์ x , y ในบรรทัดที่ 14 นั้นจะมีการเปิดใช้งาน unsafe เพื่อใช้งาน pointer ในการเข้าถึงและจัดการกับหน่วยความจำ ตัวของ pointer จะถูกสร้างขึ้นมาในบรรทัดที่ 17 ในส่วนของบรรทัดที่ 19,20 จะการใช้งาน pointer เพื่อเข้าถึงตัวแปร x กับ y ส่วนบรรทัดที่ 25 คือ syntax การใช้งาน pointer แบบ Dereferencing operator
นี่คือ code แบบที่ไม่ใช้ pointer
using System;
struct Coordinate
{
public int x;
public int y;
}
class Program
{
static void Main(string[] args)
{
Coordinate C;
C.x = 10;
C.y = 20;
Console.WriteLine("Details of Coordinate");
Console.WriteLine("Coordinate x:{0},y:{1}", C.x, C.y);
}
}
Accessing Structure Member Using Structure Pointer In C: Muskaan Mishra. (n.d.). Structure Pointer In C | Declare, Initialize & Uses (+Examples) [unstop]. unstop
Kasem Arnontavilas. (Jun 26, 2017). ทบทวนเคล็ดวิชา Pointer ให้นูบ [Medium]. Medium
Operators and expressions. (2021, April 12). Pointer related operators [Microsoft Learn]. Microsoft Learn
manasikirloskar. (2019,26 Feb). How to access structure elements using Pointers in C# [GeeksforGeeks]. GeeksforGeeks
Unsafe code and pointers. (2021, Sep 29). Unsafe code,pointer types, and function pointers [Microsoft Learn]. Microsoft Learn
Pointers in C. (n.d.).C Programming Tutorial [tutorialspoint]. tutorialspoint
Logan Jones. (2019,26 Feb). Pointers in Python: What's the Point? [Real Python]. Real Python
Somesh. (2023,25 Jun). Pointers In Java [Medium]. Medium