How to use unsafe struct with fixed int array in C#

1 Answer

0 votes
using System;

public unsafe struct AStruct
{
    public fixed int arr[64];
}

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static AStruct _astruct; 
        static void Main(string[] args)
        {
            Set();
            Print();
        }
        unsafe static void Set()
        {
            fixed (int* _arr = _astruct.arr)
            {
                _arr[0] = 88;
                _arr[12] = 123;
                _arr[34] = 345;
            }
        }

        unsafe static void Print()
        {
            fixed (int* _arr = _astruct.arr)
            {
                Console.WriteLine(_arr[0]);
                Console.WriteLine(_arr[12]);
                Console.WriteLine(_arr[34]);
            }
        }
    }
}


/*
run:

88
123
345

*/

 



answered Aug 3, 2018 by avibootz

Related questions

2 answers 261 views
261 views asked Apr 28, 2017 by avibootz
1 answer 161 views
1 answer 106 views
106 views asked Jun 21, 2024 by avibootz
1 answer 154 views
154 views asked Dec 15, 2020 by avibootz
1 answer 132 views
1 answer 141 views
141 views asked Dec 15, 2020 by avibootz
...