I am writing pthread program which contain functions as given below and I want to call another function in between one function.
void *fun1(void *arg1)
{
fun2(NULL);
}
void *fun2(void *arg)
{
fun1(NULL);
}
When I am calling another function as shown above I am getting error as shown below
error: conflicting types for 'fun2'
note: previous implicit declaration of 'fun2' was here
How can I call fun2 in between fun1
Declare :
void *fun2(void *);
before fun1
The compiler assumes the default return type as int
so you need to tell it the actual prototype before use
During compilation you have not given a prototype for fun2
like:
void * fun2(void *);
Since there is no forward declaration hence the compiler implicitly assumes it will return an int
. (I mean this is how my compiler behaves) So it will have conflicting types.
The best practice is:
void * fun1(void *);
void * fun2(void *);
void * fun1(void *arg1)
{
fun2(NULL);
//return missing
}
void * fun2(void *arg)
{
fun1(NULL);
//return missing
}
/* forward declaration */
void *fun2(void *arg);
void *fun1(void *arg1)
{
fun2(NULL);
}
void *fun2(void *arg)
{
fun1(NULL);
}
Note that, as written, you have an infinite recursion. These two functions will just call each other until the program runs out of stack and crashes.
You must declare your function before calling it.Keep habit of declaring functions in header file or top of your .c
file . You should
void *fun2(void *args);
Lets go wild and assume that you have you have used forward declarations.
fun1
(there is a misnomer) calls fun2
then that calls fun1
that calls fun2
ad infinitum - until the blessed machine runs of of stack (patience?) and gives up.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.