control reaches the end of non-void function

Discussion in 'C Programming' started by Thomas Barth, Oct 24, 2005.

  1. Thomas Barth

    Thomas Barth Guest

    Hi
    could someone explain to me why the compiler outputs a warning that the
    control reaches the end of a non void function?

    int main(void) {
    ...
    pthread_t thrCallOp, thrHangUp;
    pthread_create(&thrCallOp, NULL, &callOp, NULL);
    pthread_create(&thrHangUp, NULL, &setHangUp, NULL);

    pthread_join(thrCallOp, NULL);
    pthread_join(thrHangUp, NULL);

    ...
    return EXIT_SUCCESS;
    }

    void *callOp() {
    while(1) {
    if ('q' == ch) {
    break;
    }
    ...
    sleep(1);
    }
    } //end of non-void function


    Thanks in advance,
    T h o m a s B
     
    Thomas Barth, Oct 24, 2005
    #1
    1. Advertising

  2. Thomas Barth wrote:
    > Hi
    > could someone explain to me why the compiler outputs a warning that the
    > control reaches the end of a non void function?
    >
    > int main(void) {
    > ...
    > pthread_t thrCallOp, thrHangUp;
    > pthread_create(&thrCallOp, NULL, &callOp, NULL);
    > pthread_create(&thrHangUp, NULL, &setHangUp, NULL);
    >
    > pthread_join(thrCallOp, NULL);
    > pthread_join(thrHangUp, NULL);
    >
    > ...
    > return EXIT_SUCCESS;
    > }
    >
    > void *callOp() {
    > while(1) {
    > if ('q' == ch) {
    > break;
    > }
    > ...
    > sleep(1);
    > }
    > } //end of non-void function
    >
    >
    > Thanks in advance,
    > T h o m a s B


    You said it would return a pointer -- a void* -- and returned nothing.
    At least that is as far as I can tell from your partial code. The
    compiler assumes that you meant what you told it, and that it is an
    error to reach the end of the function without returning a void*.
    Did you mean it to be void callOp(void)?

    -David
     
    David Resnick, Oct 24, 2005
    #2
    1. Advertising

  3. In article <>,
    Thomas Barth <> wrote:
    >could someone explain to me why the compiler outputs a warning that the
    >control reaches the end of a non void function?


    >void *callOp() {
    > while(1) {
    > if ('q' == ch) {
    > break;
    > }
    > ...
    > sleep(1);
    > }
    >} //end of non-void function


    That isn't a void function, that is a void* function. The compiler
    is expecting you to return a void*, but instead you just fall off
    the end of the function.


    As to what an appropriate return value is for the start argument
    to pthread_create: you would have to inquire about that in a newsgroup
    that deals with threads. Threads are not part of the C standard, so
    we avoid talking about them in comp.lang.c .
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Oct 24, 2005
    #3
  4. Thomas Barth wrote:
    > Hi
    > could someone explain to me why the compiler outputs a warning that the
    > control reaches the end of a non void function?


    Because control reaches the end of a function that returns a ptr-to-void
    and yet you don't return anything. I would think that the warning is
    completely clear.

    [...]
    > void *callOp() {
    > while(1) {
    > if ('q' == ch) {
    > break;
    > }
    > ...
    > sleep(1);
    > }
    > } //end of non-void function
    >
    >
    > Thanks in advance,

    ^^^^^^^^^^^^^^^^^
    Bill Collector Jargon
     
    Martin Ambuhl, Oct 24, 2005
    #4
  5. Thomas Barth

    Thomas Barth Guest

    Am 10/24/2005 08:02 PM schrieb Walter Roberson:

    >
    > That isn't a void function, that is a void* function. The compiler
    > is expecting you to return a void*, but instead you just fall off
    > the end of the function.
    >


    Yes, but I dont get the warning if I comment the if block

    void *callOp() {
    while(1) {
    /*
    if ('q' == ch) {
    break;
    }
    */
    ...
    sleep(1);
    }
    } //no warning

    Regards,
    T h o m a s B
     
    Thomas Barth, Oct 24, 2005
    #5
  6. Thomas Barth <> writes:
    > Am 10/24/2005 08:02 PM schrieb Walter Roberson:
    >> That isn't a void function, that is a void* function. The compiler
    >> is expecting you to return a void*, but instead you just fall off
    >> the end of the function.

    >
    > Yes, but I dont get the warning if I comment the if block
    >
    > void *callOp() {
    > while(1) {
    > /*
    > if ('q' == ch) {
    > break;
    > }
    > */
    > ...
    > sleep(1);
    > }
    > } //no warning


    Without the break statement, it's not possible for the while loop to
    terminate -- thus you can never reach the end of the function.

    The compiler might reasonably have warned you about declaring that the
    function returns void*, when in fact it can never return anything, but
    it doesn't. Neither warning is required; it's up to the whim of the
    compiler writer to decide what warnings to issue (as long as it emits
    any diagnostics required by the standard).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 24, 2005
    #6
  7. Thomas Barth

    Skarmander Guest

    Thomas Barth wrote:
    > Am 10/24/2005 08:02 PM schrieb Walter Roberson:
    >
    >>
    >> That isn't a void function, that is a void* function. The compiler
    >> is expecting you to return a void*, but instead you just fall off
    >> the end of the function.
    >>

    >
    > Yes, but I dont get the warning if I comment the if block
    >
    > void *callOp() {
    > while(1) {
    > /*
    > if ('q' == ch) {
    > break;
    > }
    > */
    > ...
    > sleep(1);
    > }
    > } //no warning
    >


    That's because you created an infinite loop (assuming "..." isn't
    standing in for any statements that can alter control flow). Control
    never reaches the end of the function, ergo.

    Your compiler is trying to communicate! Listen to it carefully! :)

    S.
     
    Skarmander, Oct 24, 2005
    #7
  8. In article <>,
    Thomas Barth <> wrote:
    >Am 10/24/2005 08:02 PM schrieb Walter Roberson:



    >> That isn't a void function, that is a void* function. The compiler
    >> is expecting you to return a void*, but instead you just fall off
    >> the end of the function.



    >Yes, but I dont get the warning if I comment the if block


    >void *callOp() {
    > while(1) {
    > /*
    > if ('q' == ch) {
    > break;
    > }
    > */
    > ...
    > sleep(1);
    > }
    >} //no warning


    That's because the compiler is smart enough to notice that you
    have an infinite loop in the while() that can never be exitted,
    making it impossible to return anything from the function.
    --
    I am spammed, therefore I am.
     
    Walter Roberson, Oct 24, 2005
    #8
  9. Thomas Barth <> wrote:

    > void *callOp() {
    > while(1) {
    > /* if ('q' == ch) {
    > break;
    > } */
    > sleep(1);
    > }
    > } //no warning


    Your compiler is smart enough to know that when you comment out the
    portion of the code in question, there is no exit from the while loop,
    and thus control never reaches the end of your non-void function.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 24, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    568
    Jack Klein
    Apr 22, 2005
  2. John Buckley

    control reaches end of non-void function

    John Buckley, Oct 13, 2003, in forum: C Programming
    Replies:
    8
    Views:
    3,048
    Dan Pop
    Oct 14, 2003
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    851
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    419
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page