- Platform: Linux 3.2.0 x86 (Debian Wheezy)
- Compiler: GCC 4.7.2 (Debian 4.7.2-5)
I am writing a function that returns a pointer to a char. I still want to "return" an integer value that represents what error occured so the function takes as an argument a pointer to an integer that will be used as a flag. Here is an example of the declaration
char* foo(int *flag, etc.)
I want to allow flag
to be NULL if the user does not want to know what specific error happened because it may not matter (I do not know if it does or not, the idea is just theoretical). So I would write the following should an error occur,
... //Symbolic Code
if(error) //Some error has occured
{
if(flag != NULL) *flag = SOME_ERR_VALUE; //Set the flag variable
return NULL; //return NULL to show that an error occured
}
Rather than writing that code over and over again I want to write a macro for it instead. But the code is more than one statement which means I would have to put the macro in it's own code block or the macro would have to be in it's own code block already for example.
#define RETFLAG(err, ret) if(flag != NULL) *flag = err; return ret;
... //Symbolic Code
if(error) //Some error has occured
{RETFLAG(1, NULL)} //Notice the {}
The other way to do it would be,
//Notice the {} in the definition
#define RETFLAG(err, ret) {if(flag != NULL) *flag = err; return ret;}
... //Symbolic Code
if(error) //Some error has occured
RETFLAG(1, NULL)
Both of those methods would work but I do not like the seconded one because I want to put a semicolon at the end of the macro like so,
//Notice the {} in the definition
#define RETFLAG(err, ret) {if(flag != NULL) *flag = err; return ret;}
... //Symbolic Code
if(error) //Some error has occured
RETFLAG(1, NULL); //The only difference is the semicolon
I know the semicolon is unnecessary but I think that it is more readable if you have not actually looked at the definition of RETFLAG. I have already tried to use the unnecessary semicolon and my compiler did not complain and from what I can tell the function worked properly. So my question is, is the unnecessary semicolon legal syntax. If it is legal is it proper?