Set of Strings in Struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkfire
    New Member
    • Mar 2007
    • 5

    Set of Strings in Struct

    Right, I have to, for a assignment write a translator that translates Linear Temporal Logic formulae into Buchi automata in C.

    Now part of what I have at the moment is:
    Code:
    typedef struct Node {
      char *name;
      char **incoming;
      char **new;
      char **old;
      char **next;  
    } Node;
    
    Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
       Node *node = malloc(sizeof(Node));
       node->name = "Node" + j++;
       *(node->incoming) = _inc;   
       *(node->new) = _new;
       *(node->old) = _old;
       *(node->next) = _next;
       return(node);
    }
    
    Node **expand(Node *_node, Node **_nodes_Set) {
      int a = 0;
      
      if (_node->new == NULL) {
        while (_nodes_Set[a] != NULL) {
          if ((_nodes_Set[a]->old == _node->old) && (_nodes_Set[a]->next = _node->next)) {
            *(_nodes_Set[a]->incoming + 1) = *_node->incoming;
            return(_nodes_Set);
          } else {
            *(_nodes_Set + 1) = _node;
            return(expand(newNode(_node->name, (char *)_node->next, NULL, NULL), _nodes_Set));
          }      
          a++;
        }    
      }  
    }
    Node->incoming; new; old and next must all be sets of strings. And I must be able to add to them. _node_Set must be an array of Nodes to which I can add.

    My problem is, as it is here it compiles, but when I run it it throws a SIGSEGV when it gets to *(node->incoming) = _inc; in newNode();

    As you can see I have no clue how to program in this language, my experience is with C++ and I'm missing many of its build-in features.

    Please help.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    I think newNode() should be
    Code:
    Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
       Node *node = malloc(sizeof(Node));
       node->name = "Node" + j++;
       (node->incoming) = _inc;   
       (node->new) = _new;
       (node->old) = _old;
       (node->next) = _next;
       return(node);
    }
    and reason why you cannot use C++ and hence <set>

    Comment

    • darkfire
      New Member
      • Mar 2007
      • 5

      #3
      Originally posted by horace1
      I think newNode() should be
      Code:
      Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
         Node *node = malloc(sizeof(Node));
         node->name = "Node" + j++;
         (node->incoming) = _inc;   
         (node->new) = _new;
         (node->old) = _old;
         (node->next) = _next;
         return(node);
      }
      and reason why you cannot use C++ and hence <set>
      http://www.cppreference.com/cppset/index.html
      Hmmm no that won't work (and it doesn't I tried) char **increment is supposed to be an array of strings. Node->increment = _inc (as far as I understand) if you cast to get the types right will simply force Node->increment to become an array of chars.

      Edit: Typo

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by darkfire
        Hmmm no that won't work (and it doesn't I tried) char **increment is supposed to be an array of strings. Node->increment = _inc (as far as I understand) if you cast to get the types right will simply force Node->increment to become an array of chars.

        Edit: Typo
        in that case should the _inc paramater be char **, e.g. (ignore the other parameters for now)
        Code:
        Node *newNode(char **_inc, char *_new, char *_old, char *_next) {
           Node *node = malloc(sizeof(Node));
           node->incoming = _inc;   
           //*(node->new) = _new;
           //*(node->old) = _old;
           //*(node->next) = _next;
           return(node);
        }
        
        int main()
        {
            char **inc={{"aaa"},{"bbbb"},{"cccc"}};
            Node * node = newNode(inc, "bbb", "ccc", "ddd");

        Comment

        • darkfire
          New Member
          • Mar 2007
          • 5

          #5
          Hm okay right but with

          newNode(_inc, _new, _old, _next) or whatever, _inc represents the first string in the array or set or whatever. Later in expand I have to be able to add more string to it, sometimes append an array of strings to the end of it. That is why I'm using the double pointers. Is there anyway to do what I want, because with your above solution once the node is create the array is pretty much set.

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by darkfire
            Hm okay right but with

            newNode(_inc, _new, _old, _next) or whatever, _inc represents the first string in the array or set or whatever. Later in expand I have to be able to add more string to it, sometimes append an array of strings to the end of it. That is why I'm using the double pointers. Is there anyway to do what I want, because with your above solution once the node is create the array is pretty much set.
            if you are working in C++ could node->incoming be a vector<string> and you add the char* strings to it as required?

            Comment

            Working...