Search code examples
embeddedmicrocontrollermicrochipxc8

warning: (343) implicit return at end of non-void function


I am getting above warning when the pointer token is returned from the function. The below code is not complete, shorten to avoid confusion.

  char *ServerResponds()        
    {
        char copy_wifi_data[EUSART_BUFFER_SIZE];                               
        uint16_t i = 0; int tok_count = 0;            

        static char *token;
        token = strtok(copy_wifi_data, ":");               //Converting wifi data to tokens.

        while(token != NULL)
        {  
            if(tok_count == 1)
            {
                return token;
            }

            token = strtok(NULL,":");  
            tok_count++;
        } 
    }

Solution

  • The warning occurs because there is no return statement in the case that tok_count == 1 is false.

    char *ServerResponds()        
    {
        char copy_wifi_data[EUSART_BUFFER_SIZE];                               
        uint16_t i = 0; int tok_count = 0;            
    
        static char *token;
        token = strtok(copy_wifi_data, ":");               
    
        while(token != NULL)
        {  
            if(tok_count == 1)
            {
                return token;
            }
    
            token = strtok(NULL,":");  
            tok_count++;
        } 
        /* <--- there is no return if control flow reaches this point */
    
    }
    

    You could add a return 0; at the end of the function to address this, but you would have to document this behavior and be sure that the callers are prepared to handle a null return value.