0

C# does not like pointers, but I need to use them now to create a linked list, like we would do in C. The struct is simple:

    public unsafe struct Livro
    {
        public string name;
        public Livro* next;
    }

But I get the error: "Cannot take the address of, get the size of, or declare a pointer to a managed type". Any ideas?

4
  • 1
    why not List<string> ? Commented May 14, 2013 at 19:24
  • 2
    @WouterHuysentruit Or LinkedList<T> if you want a linked list ;) Commented May 14, 2013 at 19:27
  • @WouterHuysentruit If you want to insert/remove from the middle, and don't need access by index, it's much nicer in many cases. Commented May 14, 2013 at 19:32
  • @WouterHuysentruit List<T> is backed by Array. LinkedList<T> is truly double-linked list. Commented May 14, 2013 at 19:32

3 Answers 3

6

You can just use a class instead of a struct:

public class Livro
{
    public string Name { get; set; }
    public Livro Next { get; set; }
}

This will provide the proper behavior automatically.

That being said, you might want to just use the LinkedList<T> class from the framework directly.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right. I did the same with classes, but the teacher wants pointers
Pointers in C# is not a very good idea. Are you sure he want's you to do this in C# and not C/C++?
It's C#. They are learning how to use pointers. That's why I can't use a collection or objects
@user2383078: One should very rarely use pointers for anything in C#. Unless one needs to build a linked-list data structure in memory that will be used by code written in an unmanaged language like C, one should use object references rather than pointers.
4

The problem is the string declaration. You have to go low level and use char* pointers. Using unsafe comes with all the headaches of leaving the managed world...

3 Comments

I think that solves the problem. Now I am going to try to work with char pointers
@user2383078 - either go back to C/C++ or learn to do it the C# way. char pointers will make an already bad solution even worse.
Agreed. Earlier he stated, "It's C# ... but the teacher wants pointers". That teacher should be applauded for teaching pointers, but fired for using the wrong tools to do it with. He'll leave behind a bunch of students who barely understand pointers, but think it's fine to mark blocks as unsafe so they can low level with pointers and do things more "efficiently".
-1

you can do it with struct like this:

struct LinkedList
{
    public int data;
    public LinkedListPtr next;
    public class LinkedListPtr
    {
        public LinkedList list;
    }
}

or like this:

struct LinkedList
{
    public int data;
    public LinkedList[] next;
}

but it's better to just use class for the linked list instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.