•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 422,989 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 923 | Replies: 4
![]() |
•
•
Join Date: May 2006
Location: Christchurch
Posts: 43
Reputation:
Rep Power: 3
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:
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation:
Rep Power: 38
Solved Threads: 929
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
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
Location: Christchurch
Posts: 43
Reputation:
Rep Power: 3
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
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?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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



Linear Mode