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:

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

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.
}

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

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.

This article has been dead for over six months. Start a new discussion instead.