Thread: Usage of struct ** in linked lists

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    4

    Talking Usage of struct ** in linked lists

    Code:
    typedef struct gate {
    
    CallBack f;
    struct gate ** inputs;
    }Gate;

    I have a project in c programming language and I would like to ask how i can use the "inputs" in the struct. I am told that it has to be something like a list but i have only worked with lists like bellow where the *next points to the next item of the list :
    Code:
    typedef struct data {
    
    int value;
    struct data * next;
    }Data;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you understand this as being a linked list
    struct gate *inputs;

    Would you understand this as perhaps being a binary tree?
    struct gate *inputs[2];

    Do you now know what this might be?
    struct gate ** inputs;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    i would say that it is a tree of undefined size ????

  4. #4
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    But how would i use it ??
    I mean with the binary tree i could say :

    Code:
    typedef struct data {
         int val;
         struct data *next[2];
    }Data;
    
    Data * lst;
    
    Data * createData(int i){
        Data * temp;
        temp=malloc....
        temp->val=i
        temp->next=NULL;
    }
    
    lst->val=1
    lst->next[0]=createdata(1).......
    


    Or something like that.
    With the **next how will i assign it, will it be an array like i wrote *next[] ??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess somewhere, you need to determine

    int numTreeBranches = 42;


    Then later, you can do
    mygate->inputs = malloc( numTreeBranches * sizeof(*(mygate->inputs)) );

    Then you're free to do
    mygate->inputs[x] = ....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I suppose it would be helpful if you could say what the purpose of using this data structure would be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2018
    Posts
    4
    That is for the case of struct gate *inputs[ ] or for struct gates **inputs ???
    This data structure is created to represent logic gates input. I have created a program that calculates output of a circuit but can get only 2 inputs at each gate. Now this struct is needed in which Callback f is the function o f logic gate (and, or, nor...) and the **inputs needs to be the input of the gate.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it seems like you also need to store in each gate the actual number of input gates.

    You could start with
    struct gate *inputs[20];
    so you can get your program logic sorted out before adding the extra complexity of full double indirection.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Struct Communication and Linked Lists
    By CaptainMorgan in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2006, 07:44 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked Lists and their usage...
    By SyntaxBubble in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:01 AM

Tags for this Thread