By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

control reaches the end of non-void function

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

Nov 15 '05 #2

P: n/a
In article <3s************@individual.net>,
Thomas Barth <tx*****@web.de> 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
Nov 15 '05 #3

P: n/a
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
Nov 15 '05 #4

P: n/a
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
Nov 15 '05 #5

P: n/a
Thomas Barth <tx*****@web.de> 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) ks***@mib.org <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.
Nov 15 '05 #6

P: n/a
In article <3s************@individual.net>,
Thomas Barth <tx*****@web.de> 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.
Nov 15 '05 #7

P: n/a
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.
Nov 15 '05 #8

P: n/a
Thomas Barth <tx*****@web.de> 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.
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.