about struct in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 43
Reputation: yuzhang is an unknown quantity at this point 
Solved Threads: 0
yuzhang yuzhang is offline Offline
Light Poster

about struct in C

 
0
  #1
May 25th, 2006
Hi all:

I am stuck in the middle of my programme, wishing you may help.

I am writing a "quene" structure in C, and it will copy the content of a string received from a server and display it on the client monitor.

Codes:

  1. typedef struct Message_from_server message_from_server
  2.  
  3. void sqInitialise()
  4. {
  5. struct Message_from_server
  6. {
  7. char *message;
  8. message_from_server *next_message;
  9. };
  10. }
  11.  
  12. void sqEnqueue(const char *s)
  13. {
  14. Message_from_server new_message_from_server;
  15. new_message_from_server.message=s;
  16. new_message_from_server.next_message=


Here is where I got stuck, I do not really know what I should do once I create a new instance of Message_from_server. Ideally, the pointer *next_message should point to the next incoming message, but I do not know how to realize this concept.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: about struct in C

 
0
  #2
May 25th, 2006
This looks like it should be a linked list of messages. You need to allocate an object that represents the head of the linked list, then add nodes to it. One way might be like this
  1. void sqEnqueue(struct Message_from_server** head, const char *s)
  2. {
  3. struct Message_from_server* new_node = malloc(sizeof(struct Message_from_server));
  4. new_node->s = malloc(strlen(s)+1);
  5. strcpy(new_node->s,s);
  6. new_node->next_message = NULL;
  7. if( *head == NULL)
  8. {
  9. *head = new_node;
  10. }
  11. else
  12. { // add new_node to the end of the linked list
  13. // locate end of list
  14. struct Message_from_server* tail = *head;
  15. while(tail->next_message != NULL)
  16. tail = tail->next_message;
  17. // found end of list, now add the new node to it
  18. tail->next_message = new_node;
  19. }
  20. }
  21.  
  22. // this is how to call the above function
  23. int main()
  24. {
  25. struct Message_from_server* head = NULL; // top of linked list
  26. sqEnqueue( &head, "Hello");
  27. sqEnqueue( &head, "World");
  28.  
  29. // don't forget to delete the list when done with it.
  30. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 43
Reputation: yuzhang is an unknown quantity at this point 
Solved Threads: 0
yuzhang yuzhang is offline Offline
Light Poster

Re: about struct in C

 
0
  #3
May 25th, 2006
Thanks dragon. Would not have figured out without you.
:-)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 43
Reputation: yuzhang is an unknown quantity at this point 
Solved Threads: 0
yuzhang yuzhang is offline Offline
Light Poster

malloc()

 
0
  #4
May 25th, 2006
Originally Posted by Ancient Dragon
This looks like it should be a linked list of messages. You need to allocate an object that represents the head of the linked list, then add nodes to it. One way might be like this
  1. void sqEnqueue(struct Message_from_server** head, const char *s)
  2. {
  3. struct Message_from_server* new_node = malloc(sizeof(struct Message_from_server));
  4. new_node->s = malloc(strlen(s)+1);
  5. strcpy(new_node->s,s);
  6. new_node->next_message = NULL;
  7. if( *head == NULL)
  8. {
  9. *head = new_node;
  10. }
  11. else
  12. { // add new_node to the end of the linked list
  13. // locate end of list
  14. struct Message_from_server* tail = *head;
  15. while(tail->next_message != NULL)
  16. tail = tail->next_message;
  17. // found end of list, now add the new node to it
  18. tail->next_message = new_node;
  19. }
  20. }
  21.  
  22. // this is how to call the above function
  23. int main()
  24. {
  25. struct Message_from_server* head = NULL; // top of linked list
  26. sqEnqueue( &head, "Hello");
  27. sqEnqueue( &head, "World");
  28.  
  29. // don't forget to delete the list when done with it.
  30. }

Hi dragon:

You used two malloc() functions above, one is to allocate memory space for the struct, and another one is to allocate memory space for the string which is a member of the struct.
Just thinking: how does the first malloc() function know how much memory it needs to allocate for the struct before knowing the length of the string?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,457
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: malloc()

 
0
  #5
May 25th, 2006
Originally Posted by yuzhang
Just thinking: how does the first malloc() function know how much memory it needs to allocate for the struct before knowing the length of the string?
Because the struct does not contain a string, it contains a pointer, and the compiler knows how big a pointer is.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC