Accessing structure's element using Pointers

ธามนิธิศ ธนะสุข 650710555

เนื้อหาต่อไปนี้จะกล่าวถึง การเข้าถึง ตัวแปรต่างๆใน structure โดนใช้ pointers


ก่อนที่เราจะมาพูดถึงเรื่อง การ acess เข้า structure นั้นเรามาทำความรู้จักอีก 1 ตัวละครสำคัญในการที่จะทำให้เรานั้น สามารถใช้ pointer ได้ใน C# กันดี คนๆนั้นก็คือ...

Unsafe

โดยทั่วไปแล้วการเขียน code ใน C# จะเป็น code ที่ได้รับการ verify จากทาง .NET แล้วว่าเป็น code ที่ปลอดภัย โดยปลอดภัยในที่นี้นั้นหมายถึง จะไม่มีการเข้าไปยุ่งเกี่ยวกับตัวหน่วยความจำโดยตรง โดยใช้ตัว pointer แต่จะเป็นการ สร้างตัว object มาจัดการ unsafe code ไม่ได้หมายความว่าเป็น code ที่เป็นอันตรายเสมอไป เป็นเพียงแค่ ไม่สารถตรวจสอบความปลอดภัยโดย .NET ได้ ดังนั้นแล้วการจะใช้ pointer เพื่อไปยุ่งเกี่ยวกับหน่วยความจำโดยตรงแล้ว จึงจำเป็นต้องมีการใช้งาน keyword unsafe ใน code ก่อน


Pointer

pointer หรือ ตัวชี้ คือ ตัวแปรที่เก็บค่า address ของข้อมูล และ ตัวแปรต่างๆ ในภาษา C# Pointer ถือเป็น data type แบบที่สาม (สองแบบแรกคือ value type และ reference type) pointer คือตัวแปรที่เราใช้งานได้ภายในบล็อก unsafe เท่านั้น

ภาพนี้แสดงความแตกต่างระหว่างตัวแปรธรรมดากับตัวแปรแบบ pointer

การ Acess เข้า Structure โดยใช้ pointers

การเข้าถึง structure ในภาษา C# โดยใช้ pointers นั้นสามารถทำได้หลักๆ 2 วิธี

1 ) การใช้ Arrow operator

Syntax:

PointerName->memberName;

Example:

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 

คำอธิบายตัวอย่าง : ใน code นี้จะมีการสร้าง struct Coordinate ขึ้นมาโดยมีฟิลด์ x , y ในบรรทัดที่ 20 นั้นจะมีการเปิดใช้งาน unsafe เพื่อใช้งาน pointer ในการเข้าถึงและจัดการกับหน่วยความจำ ตัวของ pointer จะถูกสร้างขึ้นมาในบรรทัดที่ 25,26 ส่วนบรรทัดที่ 31 และ 35 คือ syntax การใช้งาน pointer แบบ Arrow operator

นี่คือ code แบบที่ไม่ใช้ pointer

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 

คำอธิบายตัวอย่าง : ใน 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);
    }
}


เปรียบเทียบการใช้ Pointers ในการเข้าถึง Structer ของ C# กับภาษาต่าง ๆ

using System;

struct Item
{
    public int itemID;
    public int price;
    
    public Item(int id, int price)
    {
        this.itemID = id;
        this.price = price;
    }
}

class Program
{
    static void Main(string[] args)
    {
    	unsafe{
        	Item I1;
        	Item* ptr = &I1;
        	(*ptr).itemID = 1;
        	(*ptr).price = 300;
        
        	Item I2 = new Item(2, 40);
                Item* I2_ptr = &I2;
                
                //Dereferencing operator
        	Console.WriteLine("Details of Item");
        	Console.WriteLine("Item  ID:{0},price:{1}", 
                                (*ptr).itemID, (*ptr).price);
                
                //Arrow operator
                Console.WriteLine("Details of Item");
        	Console.WriteLine("Item  ID:{0},price:{1}", 
                    	I2_ptr -> itemID, I2_ptr -> price);
        }
    }
}


Presentation:

Video:


Reference

  • ตัวแปร และตัวแปรแบบ pointer ในภาษา C# : ลาภลอย วานิชอังกูร. (2013). เรียนรู้ด้วยตนเอง OOP C# ASP.NET. กรุงเทพฯ : ซีเอ็ดยูเคชั่น. (ISBN(e-book) : 978-616-08-0678-2). thaioop

  • 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

Last updated