Search code examples
clinked-listmallocdynamic-memory-allocation

how to allocate space in a struct


I have the following:

int main(void)
{
    typedef struct node
   {
       char * word = malloc(46 * sizeof(char));
       struct node *left;
       struct node *right;
   } node;
 }

but it produces:

test2.c:12:16: error: expected ';' at end of declaration list
char * word = malloc(46 * sizeof(char));

How and where do you allocate the memory for the struct?


Solution

  • You may not initialize data members of a structure within its definition in C.

    You need to create an object of the structure type and initialize it in its declaration or assign values to its data members separately.

    For example

    int main(void)
    {
        typedef struct node
       {
           char * word;
           struct node *left;
           struct node *right;
       } node;
    
       node *new_node = malloc( sizeof( node ) );
       new_node->word = malloc( 46 * sizeof( char ) );
       strcpy( new_node->word, "Some word or words" );
       new_node->left = NULL;
       new_node->right = NULL;
     }
    

    Here is a demonstrative program.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        typedef struct node
       {
           char * word;
           struct node *left;
           struct node *right;
       } node;
    
       node *new_node = malloc( sizeof( node ) );
       new_node->word = malloc( 46 * sizeof( char ) );
       strcpy( new_node->word, "Some word or words" );
       new_node->left = NULL;
       new_node->right = NULL;
       
       puts( new_node->word );
       
       free( new_node->word );
       free( new_node );
     }
    

    Its output is

    Some word or words
    

    Pay attention to that you need to define the structure in the file scope if you are going to use it in functions apart from main.

    Here is a more meaningful demonstrative program.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node
    {
        char * word;
        struct node *left;
        struct node *right;
    } node;
    
    typedef struct list
    {
        node *head;
        node *tail;
    } list;
    
    int push_back( list *list, const char *word )
    {
        node *new_node = malloc( sizeof( node ) );
        int success = new_node != NULL;
        
        if ( success )
        {
            new_node->word = malloc( strlen( word ) + 1 );
            success = new_node->word != NULL;
    
            if ( success )
            {
                strcpy( new_node->word, word );
                new_node->right = NULL;
                new_node->left = list->tail;
                    
                if ( list->head == NULL )
                {
                    list->head = new_node;
                }
                else
                {
                    list->tail->right = new_node;
                }
                    
                list->tail = new_node;
            }
            else
            {
                free( new_node );
            }
        }
    
        return success;
    }
    
    void free_list( list *list )
    {
        while ( list->head )
        {
            node *current = list->head;
            list->head = list->head->right;
            free( current->word );
            free( current );
        }
        
        list->tail = NULL;
    }
    
    void display_list( const list *list )
    {
        for ( const node *current = list->head; current != NULL; current = current->right )
        {
            printf( "%s -> ", current->word );
        }
        
        puts( "null" );
    }
    
    int main(void)
    {
        list list = { .head = NULL, .tail = NULL };
    
        push_back( &list, "Hello doubly-linked list!" );
        
        display_list( &list );
        
        free_list( &list );
    }
    

    Its output is

    Hello doubly-linked list! -> null