| | |
about struct in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 43
Reputation:
Solved Threads: 0
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:
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
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:
C Syntax (Toggle Plain Text)
typedef struct Message_from_server message_from_server void sqInitialise() { struct Message_from_server { char *message; message_from_server *next_message; }; } void sqEnqueue(const char *s) { Message_from_server new_message_from_server; new_message_from_server.message=s; 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
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
C Syntax (Toggle Plain Text)
void sqEnqueue(struct Message_from_server** head, const char *s) { struct Message_from_server* new_node = malloc(sizeof(struct Message_from_server)); new_node->s = malloc(strlen(s)+1); strcpy(new_node->s,s); new_node->next_message = NULL; if( *head == NULL) { *head = new_node; } else { // add new_node to the end of the linked list // locate end of list struct Message_from_server* tail = *head; while(tail->next_message != NULL) tail = tail->next_message; // found end of list, now add the new node to it tail->next_message = new_node; } } // this is how to call the above function int main() { struct Message_from_server* head = NULL; // top of linked list sqEnqueue( &head, "Hello"); sqEnqueue( &head, "World"); // don't forget to delete the list when done with it. }
•
•
Join Date: May 2006
Posts: 43
Reputation:
Solved Threads: 0
•
•
•
•
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
C Syntax (Toggle Plain Text)
void sqEnqueue(struct Message_from_server** head, const char *s) { struct Message_from_server* new_node = malloc(sizeof(struct Message_from_server)); new_node->s = malloc(strlen(s)+1); strcpy(new_node->s,s); new_node->next_message = NULL; if( *head == NULL) { *head = new_node; } else { // add new_node to the end of the linked list // locate end of list struct Message_from_server* tail = *head; while(tail->next_message != NULL) tail = tail->next_message; // found end of list, now add the new node to it tail->next_message = new_node; } } // this is how to call the above function int main() { struct Message_from_server* head = NULL; // top of linked list sqEnqueue( &head, "Hello"); sqEnqueue( &head, "World"); // don't forget to delete the list when done with it. }
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
•
•
•
•
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?
"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
![]() |
Similar Threads
- Converting Struct to class lost with pointers (C++)
- A mistake about struct (C)
- problem reading text file to struct (C++)
- fstream and struct question (C++)
- struct error (!?) (C)
- struct dynamic 2d array alloc (C)
- struct type redefinition (C++)
Other Threads in the C Forum
- Previous Thread: can sum1 help me with sum piping
- Next Thread: XML to Text file
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






