lauvilu
lauvilu

Reputation: 17

warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’

when I compile my code it shows me 3 warnings:

warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ 
warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’
 warning: format ‘%hhu’ expects argument of type ‘int’, but argument 3 has type ‘unsigned char *’ 

Here's the code:

typedef struct
{
int c; 
int l; 
unsigned char **matrizPixels; 
} PGM;

 void salvaPGM(PGM *img, char* saida)

 {
int i,j;

FILE *arq;

arq = fopen(saida,"w");

fprintf(arq,"P2\n");
fprintf(arq,"%d ", &img->c);
fprintf(arq,"%d ", &img->l);
fprintf(arq,"255\n");

for(i = 0; i++; i < img->l )
{
    for (j = 0; j++; j < img->c)
    {
        fprintf(arq,"%hhu ",&img->matrizPixels[i][j]);
    }
     fprintf(arq,"\n");
}

   fclose (arq);
 }

Upvotes: 1

Views: 14506

Answers (4)

JackCColeman
JackCColeman

Reputation: 3807

img is a pointer to the PGM structure. The statement img->c dereferences the pointer and addresses the c variable within PGM. You could also code: (*img).c the parens are necessary because the dereference operator * has a very low precedence and you want it bound to img so that *img is a dereferenced structure.

& is the address operator and it creates a pointer. The statement &img->c produces a pointer to c, but printf wants c not a pointer to it.

Thus, write img->c (per other answers) or (*img).c

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224884

You have several printf statements with mismatched format strings and arguments. Those mismatches will cause your program to have undefined behaviour, so the compiler is warning you. You need to change:

fprintf(arq,"%d ", &img->c);
fprintf(arq,"%d ", &img->l);

to:

fprintf(arq,"%d ", img->c);
fprintf(arq,"%d ", img->l);

And later on:

    fprintf(arq,"%hhu ",&img->matrizPixels[i][j]);

to:

    fprintf(arq,"%d ",img->matrizPixels[i][j]);

I removed the hh in the last one, since it's unnecessary. I also changed from %u to %d to match the default argument promotion that will take place.

As an aside, it appears you have your for loop expressions in the wrong order. It's a bit weird to have:

for(i = 0; i++; i < img->l )

I expect you meant that to be:

for(i = 0; i < img->l; i++)

Upvotes: 2

Femaref
Femaref

Reputation: 61437

The printf format string specifies an int, but you are providing an int *, a pointer to an int.

You need to drop the & in front of the variables. &x results in a pointer to x, you just want x.

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

Drop the & as %d is used int

Something like this:-

fprintf(arq,"%d ", img->c);
fprintf(arq,"%d ", img->l);

and

 fprintf(arq,"%hhu ",img->matrizPixels[i][j]);

Upvotes: 0

Related Questions