Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c, control reaches end of non-void function in c

I have dispatchQueue.c:215: warning: control reaches end of non-void function warning from the code below..
Can anyone please explain why?

void *dispatcher_threadloop(void *arg){

//thread loop of the dispatch thread- pass the tast to one of worker thread

dispatch_queue_thread_t *dThread = arg;
dispatch_queue_t *dQueue;
dQueue = dThread->queue;

if (dQueue->HEAD!=NULL){
    for(;;){
        printf("test");
        sem_wait(&(dQueue->queue_task_semaphore));
        dThread->current_task = dQueue->HEAD;
        dQueue->HEAD =  dQueue->HEAD->next;
        dQueue->HEAD->prev = NULL;
        sem_post(&(dQueue->queue_task_semaphore));
        break;
        //TODO
    }
}

}

like image 900
Leanne Avatar asked Aug 10 '11 07:08

Leanne


People also ask

What is non-void function in C?

If the function is non-void,it means it has to return something before reaching the end of function block[ _} ]. So, when we give only if and else-if statements the compiler cannot tell from that code,that any of these statements will be evaluated to true and return something.

How do you end a void function?

1) A Void Function Can Return: We can simply write a return statement in a void fun(). In fact, it is considered a good practice (for readability of code) to write a return; statement to indicate the end of the function.

How do you exit a void function in C?

Use return; instead of return(0); to exit a void function.

Why do C programs start with void?

The literal meaning of void is empty or blank. In C, void can be used as a data type that represents no data.


2 Answers

Because you are declaring it void * (not void) and not returning anything. Return NULL if you don’t need any return value.

void *dispatcher_threadloop(void *arg)
like image 62
cnicutar Avatar answered Sep 28 '22 10:09

cnicutar


Well, imagine what happens if dQueue->HEAD is NULL: the if won't be entered, so you get to the end of the function which is supposed to return a void* - but you don't return anything.

Try returning some sensible value at the bottom of your function to fix this. Or add an assertion which states that this code should be unreachable, like:

assert( !"Unreachable code hit" );
like image 22
Frerich Raabe Avatar answered Sep 28 '22 08:09

Frerich Raabe