GCC: why a -Wformat warning for this fprintf?
We cleaned up a large number of warnings. In several cases, we replaced %s with %d. Everything seemed OK until we realized that the generated code wouldn't compile - strings had been replaced by numbers.
NOTE: I had the wrong revision checked out when I did开发者_开发知识库 the copy&paste. The fprintf string has been edited!
GCC output
fedex_plus/classes.c:2425:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
lines 2424 and 2425:
fprintf(file, "\n//\t%s_ptr create_TIE();\n\tIDL_Application_instance_ptr create_TIE();\n",
ENTITYget_CORBAname(entity));
function ENTITYget_CORBAname:
const char *
ENTITYget_CORBAname (Entity ent)
{
static char newname [BUFSIZ];
strcpy( newname, ENTITYget_name(ent) );
newname[0] = ToUpper (newname [0]);
return newname;
}
My bet is that no declaration of ENTITYget_CORBAname
is available at the fprintf
spot. Should this be the case, it defaults to the implicit signature:
int ENTITYget_CORBAname(...);
hence the warning about the result type being an int
.
Check your code with -Wimplicit
(implied by -Wall
), or try to put
const char *ENTITYget_CORBAname (Entity ent);
before the fprintf
line and check if the warning disappears. If it does, then you're likely missing a header.
If you want to print a pointer value, use %p
, if you want to print the string returned from the function, use %s
. Don't use %d
in any of this cases. See here.
I assume that ENTITY_GETCorbaName()
returns a string, so if you want to print that, use %s
. If you want to print it as pointer, use %p
.
use %s
in your printf
to print that string or use %p
in your printf
to show pointer value you can also use %lld
to prints its value
精彩评论