Search code examples
cpointerslinked-listc-stringssingly-linked-list

C - Return pointer of first element in linked list (string)


My function needs to return the pointer of the first element in the linked list String s after adding a character.

typedef struct String
{
    char cChar; //data of type char stored in a node
    struct String* strNext; //pointer to the next node of the linked list
}String;

String create ()
{
    String* strTemp = NULL;

    //dynamic memory allocation
    strTemp = (String*)malloc(sizeof(String));
    //to make sure returned character is null after creation
    strTemp->cChar = '\0';
    strTemp->strNext = NULL;

    return *strTemp;
}

void concat(String *a, String *b){
    if (a->strNext == NULL)
        a->strNext = b;
    else
        concat(a->strNext,b);
}

/**
 * @brief Adds a character c to the end of the String s
 * 
 * @param String s : string to be manipulated
          char c : character to be added
          
 * @return String : the pointer of the first element in String s
 */
String add (String s, char c){
    String* strTemp = NULL;
    strTemp = (String*)malloc(sizeof(String));
    strTemp->cChar = c;
    strTemp->strNext = '\0';
    concat(&s, strTemp);
    
    return s; <--- this is where it should return
} 

The string needs to be a linked list and the add function is called like this: add(s1, 'H');

I can't get a hold of linked lists in C.


Solution

  • For starters the function create produces a memory leak because within the function a memory is allocated and not freed and the function returns a copy of the created object.

    You should declare and define the function at least like

    String * create( void )
    {
        String* strTemp = NULL;
    
        //dynamic memory allocation
        strTemp = (String*)malloc(sizeof(String));
        //to make sure returned character is null after creation
        if ( strTemp != NULL )
        {
            strTemp->cChar = '\0';
            strTemp->strNext = NULL;
        }
    
        return strTemp;
    }
    

    The function concat is confusing. It should create a new list based on the passed two lists.

    The function add invokes undefined behavior because it uses a null pointer

    String* strTemp = NULL;
    

    to access a memory

    strTemp->cChar = c;
    

    The function should look the following way

    int add (String *s, char c )
    {
        while ( s->next != NULL ) s = s->next;
    
        s->next = malloc( sizeof( String ) );
        int success = s->next != NULL;
    
        if ( success )
        {
            s->cChar = c;
            s->next->cChar = '\0';
            s->next->next = NULL;
        }
    
        return success;
    }
    

    It is supposed that the list can not be a null pointer.

    If you want that the function create would return an object of the type String then the function will look like

    String create( void )
    {
        String strTemp = { .cChar = '\0', .next = NULL };
    
        return strTemp;
    }