C语言 c、控制到达c中非void函数的结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7007231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:20:38  来源:igfitidea点击:

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

cfunctionwarningsvoid

提问by Leanne

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

dispatchQueue.c:215: warning: control reaches end of non-void function从下面的代码中收到警告..
谁能解释一下原因?

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
    }
}

}

}

回答by cnicutar

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

因为你是在声明它void *(不是void)并且没有返回任何东西。返回NULL如果你不需要任何返回值。

void *dispatcher_threadloop(void *arg)

回答by Frerich Raabe

Well, imagine what happens if dQueue->HEADis NULL: the ifwon'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.

好吧,想象一下如果dQueue->HEAD是会发生什么NULLif不会被输入,所以你到达了应该返回 a 的函数的末尾void*- 但你没有返回任何东西。

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" );

回答by Leanne

The signature for your function indicates it returns a void *, which is a pointer and is different than void.

您的函数的签名表明它返回 a void *,它是一个指针,与void.

If your function isn't meant to return anything, use void.

如果您的函数不打算返回任何内容,请使用void.

回答by Zaxter

This is an old question, but if someone's trying to figure this out (for pthreads), you MAY want to return:

这是一个老问题,但如果有人试图解决这个问题(对于 pthreads),您可能想要返回:

  • pthread_exit(void *retval)(For joinable threads)
  • pthread_exit(NULL)(For detached threads)
  • pthread_exit(void *retval)(对于可连接螺纹)
  • pthread_exit(NULL)(对于分离螺纹)

回答by hari

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/topic/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpm1/m1rhnvf.html

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/topic/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpm1/m1rhnvf.html

This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.

此警告类似于无值返回中描述的警告。如果控制到达函数的末尾并且没有遇到返回,GCC 假定返回没有返回值。但是,为此,该函数需要一个返回值。在函数的末尾,添加返回合适的返回值的 return 语句,即使控制从未到达那里。

From the prototype it seems you want to return something.

从原型来看,您似乎想返回一些东西。