title: How do C pointers correlate to C# in this example?
body: <p>I am currently trying to port a library from C over to C#, most of it has went smoothly so far but I've hit a roadblock when it comes to converting struct pointers from C.</p>
  
  <p>Here is the C code that I am trying to convert:</p>
  
  <pre><code>//Queue.c File
  struct queue
  {
      struct queue* next;
      void* e;
  };
  
  struct queue* create_queue_node(void* element)
  {
      struct queue* tmp = malloc_node();
      tmp-&gt;e = element;
      return tmp;
  }
  
  void append_queue_node(struct queue* node, struct queue* after) // adds node next to after
  {
      node-&gt;next = after-&gt;next;
      after-&gt;next = node;
  }
  
  
  //Allocator.c File
  #define QUEUE_CHUNK 65536
  #define MOVE_CHUNK 65536
  
  struct move* alloc_moves = 0; // allocated moves
  int last_move = MOVE_CHUNK;
  struct move freed_moves;
  
  struct queue* alloc_nodes = 0; // allocated nodes 
  int last_node = QUEUE_CHUNK;
  struct queue freed_nodes;
  
  void inicialize_allocator()
  {
      freed_moves.parent = 0;
      freed_nodes.next = 0;
  }
  
  struct queue* malloc_node()
  {
      if (last_node &lt; QUEUE_CHUNK)
          return &amp;alloc_nodes[last_node++];
      else
      {
          alloc_nodes = malloc(sizeof(struct queue)*QUEUE_CHUNK);
          last_node = 1;
          return alloc_nodes;
      }
  }
  </code></pre>
  
  <p>Now I understand that C# count a Class as a reference type so <code>(queue* node)</code> in C is the same as <code>(Queue node)</code> in C# (if I understand correctly). So here is the C# code I have so far:</p>
  
  <pre><code>//Queue.cs File
  public class Queue
      {
          public Queue next;
          public IntPtr e;
  
          public Queue createQueueNode(IntPtr element)
          {
              Queue tmp = mallocNode();
              tmp.e = element;
              return tmp;
          }
  
          public void appendQueueNode(Queue node, Queue after)
          {
              node.next = after.next;
              after.next = node;
          }
  
          public Queue removeQueueNode(Queue before)
          {
              Queue tmp = before.next;
              before.next = before.next.next;
              return tmp;
          }
  
          public bool isQueueEmpty(Queue head)
          {
              return head.next == null;
          }
      }
  
  //Allocator.cs File
  public static class Allocator
  {
      public static Move allocMoves = null;
      public static int lastMove = Global.MOVECHUNK;
      public static Move freedMoves = null;
  
      public static Queue allocNodes = null;
      public static int lastNode = Global.QUEUECHUNK;
      public static Queue freedNodes = null;
  
      public static void initializeAllocator()
      {
          freedMoves.parent = null;
          freedNodes.next = null;
      }
  
      public static Queue mallocNode()
      {
          if(lastNode &lt; Global.QUEUECHUNK)
          {
              return //?
          }
          else
          {
              allocNodes = new //?
              lastNode = 1;
              return allocNodes;
          }
      }
  }
  </code></pre>
  
  <p>I am unsure how the allocation of the allocNodes variable should be done though, or it's usage on the return line. How should something like this be implemented in C#?</p>