User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2006
Location: Christchurch
Posts: 43
Reputation: yuzhang is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
yuzhang yuzhang is offline Offline
Light Poster

Help about struct in C

  #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:

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: about struct in C

  #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
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.
} 
Reply With Quote  
Join Date: May 2006
Location: Christchurch
Posts: 43
Reputation: yuzhang is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
yuzhang yuzhang is offline Offline
Light Poster

Re: about struct in C

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

Question malloc()

  #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
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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,614
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: malloc()

  #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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC