By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Set of Strings in Struct

P: 5
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:
Expand|Select|Wrap|Line Numbers
  1. typedef struct Node {
  2.   char *name;
  3.   char **incoming;
  4.   char **new;
  5.   char **old;
  6.   char **next;  
  7. } Node;
  8.  
  9. Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
  10.    Node *node = malloc(sizeof(Node));
  11.    node->name = "Node" + j++;
  12.    *(node->incoming) = _inc;   
  13.    *(node->new) = _new;
  14.    *(node->old) = _old;
  15.    *(node->next) = _next;
  16.    return(node);
  17. }
  18.  
  19. Node **expand(Node *_node, Node **_nodes_Set) {
  20.   int a = 0;
  21.  
  22.   if (_node->new == NULL) {
  23.     while (_nodes_Set[a] != NULL) {
  24.       if ((_nodes_Set[a]->old == _node->old) && (_nodes_Set[a]->next = _node->next)) {
  25.         *(_nodes_Set[a]->incoming + 1) = *_node->incoming;
  26.         return(_nodes_Set);
  27.       } else {
  28.         *(_nodes_Set + 1) = _node;
  29.         return(expand(newNode(_node->name, (char *)_node->next, NULL, NULL), _nodes_Set));
  30.       }      
  31.       a++;
  32.     }    
  33.   }  
  34. }
  35.  
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.
Mar 7 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,510
I think newNode() should be
Expand|Select|Wrap|Line Numbers
  1. Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
  2.    Node *node = malloc(sizeof(Node));
  3.    node->name = "Node" + j++;
  4.    (node->incoming) = _inc;   
  5.    (node->new) = _new;
  6.    (node->old) = _old;
  7.    (node->next) = _next;
  8.    return(node);
  9. }
  10.  
and reason why you cannot use C++ and hence <set>
http://www.cppreference.com/cppset/index.html
Mar 7 '07 #2

P: 5
I think newNode() should be
Expand|Select|Wrap|Line Numbers
  1. Node *newNode(char *_inc, char *_new, char *_old, char *_next) {
  2.    Node *node = malloc(sizeof(Node));
  3.    node->name = "Node" + j++;
  4.    (node->incoming) = _inc;   
  5.    (node->new) = _new;
  6.    (node->old) = _old;
  7.    (node->next) = _next;
  8.    return(node);
  9. }
  10.  
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
Mar 7 '07 #3

Expert 100+
P: 1,510
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)
Expand|Select|Wrap|Line Numbers
  1. Node *newNode(char **_inc, char *_new, char *_old, char *_next) {
  2.    Node *node = malloc(sizeof(Node));
  3.    node->incoming = _inc;   
  4.    //*(node->new) = _new;
  5.    //*(node->old) = _old;
  6.    //*(node->next) = _next;
  7.    return(node);
  8. }
  9.  
  10. int main()
  11. {
  12.     char **inc={{"aaa"},{"bbbb"},{"cccc"}};
  13.     Node * node = newNode(inc, "bbb", "ccc", "ddd");
  14.  
Mar 7 '07 #4

P: 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.
Mar 7 '07 #5

Expert 100+
P: 1,510
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?
Mar 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.