control reaches the end of non-void function

T

Thomas Barth

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
 
D

David Resnick

Thomas said:
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
 
W

Walter Roberson

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 .
 
M

Martin Ambuhl

Thomas said:
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
 
T

Thomas Barth

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
 
K

Keith Thompson

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

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).
 
S

Skarmander

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


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.
 
W

Walter Roberson

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


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.
 
C

Christopher Benson-Manica

Thomas Barth said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top