Reputation: 173
#include <stdio.h>
#include <string.h>
void prints(char *menu)
{
int p = 0;
p = navigation(p, menu);
}
int navigation(int p, char *menu)
{
return p;
}
void main(void)
{
char *menu[] = {"data", "coming", "here"};
prints(*menu);
printf("\n");
}
How can i fix this the warning is:
Warning 1 warning C4013: 'navigation' undefined; assuming extern returning int i:*\testingzone\index.c 9
Upvotes: 0
Views: 15824
Reputation: 490108
Along with the other suggestions you've received to declare the function, in this case you can just rearrange the function definitions:
#include <stdio.h>
#include <string.h>
int navigation(int p, char *menu)
{
return p;
}
void prints(char *menu)
{
int p = 0;
p = navigation(p, menu);
}
int main(void)
{
char *menu[] = {"data", "coming", "here"};
prints(*menu);
printf("\n");
return 0;
}
This isn't always possible though. In particular, you need to use a declaration when/if you have mutually recursive functions (A calls B and B also calls A).
I've also fixed the undefined behavior from defining the wrong return type from main
.
Upvotes: 2
Reputation: 173
#include <stdio.h>
#include <string.h>
int navigation(int p, char *menu);
void prints(char *menu);
void main(void)
{
char *menu[] = {"data", "coming", "here"};
prints(*menu);
printf("\n");
}
int navigation(int p, char *menu)
{
return p;
}
void prints(char *menu)
{
int p = 0;
p = navigation(p, menu);
}
Even making it looks like again show the error on p = navigation(p, menu)
Upvotes: 0
Reputation: 320421
Declare functions before calling them. In your case you make an attempt to call navigation
before declaring it. It is legal in C89/90, but usually results in compiler warning. C99 specification of C language requires declaration, meaning that your warning will turn into an error in C99. So, if you just do
int navigation(); /* declaration */
before attempting to call navigation
, the warning will disappear.
Moreover, it is a good idea to not just declare functions before calling them, but actually declare them with a prototype. C99 does not require this, but it is nevertheless a very good practice. I.e. it is better to declare navigation
as
int navigation(int, char *); /* prototype */
or
int navigation(int p, char *menu); /* prototype */
depending on your tastes.
Note, that the text of your compiler warning is misleading. It seems to suggest that a definition of the functions is required before the call. In reality, a mere non-defining declaration is perfectly sufficient.
Upvotes: 1
Reputation: 45057
Add the prototype int navigation(int p, char *menu);
somewhere above the first time navigation
is used. You get that error when a function is used before it is defined.
Upvotes: 0
Reputation: 11214
It means what it says; you're trying to use navigation(), which does not yet exist. You need to move navigation() to be the first method in the file. Alternatively, and preferably, you should prototype all functions at the beginning of the file.
Upvotes: 0