Mark
Mark

Reputation: 1053

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;
}

Upvotes: 3

Views: 11959

Answers (4)

Rudy Velthuis
Rudy Velthuis

Reputation: 28846

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.

Upvotes: 1

Jeegar Patel
Jeegar Patel

Reputation: 27240

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

Upvotes: 0

Alexandre C.
Alexandre C.

Reputation: 56996

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.

Upvotes: 11

MByD
MByD

Reputation: 137442

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.

Upvotes: 1

Related Questions