C 5.71 functions Prototype / declaration error
Terry Swanborough (455) 53 posts |
Hi I am using the new compiler version 5.71 and have the following problem. if I create a function as follows :- int Scan_Test(int,int,FILE); int Scan_Test(int v1,int v2, FILE *tempfile) The compiler responds with the following error :- if I use the prototype :- How do I stop the compiler from complaining. TIA Terry |
Jeffrey Lee (213) 6048 posts |
The problem is that you’ve missed off the * in the prototype. So instead of declaring a function which takes a pointer to a FILE struct, you’ve declared a function which takes a FILE struct by value (i.e. a copy of it). For this to work the compiler needs to have seen the full definition of FILE, so that it can work out its size (so that it knows how much stack space is needed to store it when passing it into the function). But the full definition hasn’t been seen, hence the “…formal ‘FILE’ needs type or class” error. Exactly why the error message starts off with a “prototype and old style parameters mixed” error I’m not sure, but the missing * is the cause of the problems. The basic rule you should follow with function prototypes in C/C++ is to make them exactly the same as the declaration. The variable names can be omitted if you wish (the compiler doesn’t need to know them for the prototype), but keeping them there will be a useful reminder for any fleshy types who want to use your code (including yourself). Note that there are some times when a function prototype shouldn’t be the same as the declaration, e.g. when declaring static class member functions, but more often than not the declaration must be the same as the prototype for things to work correctly, if at all. |
Terry Swanborough (455) 53 posts |
Hi I did try that before I posted an got the following errors? :-( int Scan_Test(int,int,FILE*); Causes the following:- prototype and old style parameters mixed expected ‘;’ or ‘,’ – inserted ‘;’ before ‘*’ I also tried int Scan_Test(int,int,FILE *tempfile); but that does not work either ? TIA Terry |
Jeffrey Lee (213) 6048 posts |
In that case you probably forgot to #include <stdio.h>. Even though you’re only passing around a pointer to it, the compiler still wants to know what type it is. |
Terry Swanborough (455) 53 posts |
Hi Thanks for that, so easy when someone else points it out. I had moved some includes around and moved stdio.h I could get to compile with errors but have found that these errors Thanks again for your help Terry |
Steve Revill (20) 1361 posts |
For what it’s worth, I don’t see any value in doing the old-style prototypes which don’t name the parameters; it’s not beneficial to anyone (even you in the future) who needs to use them because they have to go off and look at the function implementation to understand it – it’s far better to give your function parameters sensible names and then use an identical function signature as your prototype. |