c- format “%d” expects argument of type 'int *

2019-09-21 17:39发布

问题:

I'm new with C programming and I need to get images from two .dat files and compare if both of them are equal. The files first have the number of images, and then some images. I need to return 1 if all of the images are the same.

Pic of one of the .dat files:

F1.dat

The first 20 means 20 "groups" of images. Then 3 4 would mean 3x4 matrix and the rest would be the matrix. The same with the other 19 matrices.

I need to go comparing the first matrix of the first file with the first one from the other file and so on.

This is the code I wrote:

#include <stdio.h>
#include <stdlib.h>

struct irudia{
    int w,h;
    unsigned char im[16][16];
};

int berdinak (struct irudia *im1, struct irudia *im2){
int berdin = 1;
if ((im1->w != im2->w) || (im1->h != im2->h)){
    return 0;
}
for (int i = 0; i < im1 -> h; i++){
    for (int j = 0; j < im1 -> w; j++){
        if (im1->im[i][j] != im2->im[i][j]){
            berdin = 0;
        }
    }
}
return berdin;
}

int main (int argc, char *argv[]){
int n1, n2;
int berdin = 1;
struct irudia i1, i2;
if (argc != 3){
    printf("Errorea parametroetan. Programa amaitu da.\n");
    exit(-1);
}
FILE *fitx1 = fopen(argv[1], "r");
FILE *fitx2 = fopen(argv[2], "r");
fscanf(fitx1, "%d", &n1);
fscanf(fitx2, "%d", &n2);
if (n1 != n2){
    return 0;
}
struct irudia *p1 = &i1;
struct irudia *p2 = &i2;
while (!feof(fitx1) && berdin == 1){
    fscanf (fitx1, "%d", &i1.h);
    fscanf (fitx1, "%d", &i1.w);
    fscanf (fitx2, "%d", &i2.h);
    fscanf (fitx2, "%d", &i2.w);
    for (int i = 0; i < i1.h; i++){
        for (int j = 0; j < i1.w; j++){
            fscanf (fitx1, "%d", (unsigned char)(i1.im[i][j]));
        }
    }
    for (int i = 0; i < i2.h; i++){
        for (int j = 0; j < i2.w; j++){
            fscanf (fitx2, "%d", (unsigned char)(i2.im[i][j]));
        }
    }
    berdin = berdinak (p1, p2);
}
    fclose (fitx1);
    fclose (fitx2);
    return berdin;
}

I don't need you to understand the code or anything like that. I need to know why I am getting a warning when I do

fscanf (fitx1, "%d", (unsigned char)(i1.im[i][j]));

回答1:

fscanf reads the value into the provided address. Warnings are your friends. You are getting that particular warning because the compiler is telling you that it will interpret that value of i1.im[i][j] as an address and try to dereference it in order to put the scanned value inside. The likely result would be a segfault.

Instead try:

fscanf (fitx1, "%hhu", &(i1.im[i][j]));


回答2:

In these lines

fscanf (fitx1, "%d", (unsigned char)(i1.im[i][j]));
...
fscanf (fitx2, "%d", (unsigned char)(i2.im[i][j]));

you are supposed to pass the address of an int variable, but you are casting to the type it already is, which is not a address.

Your data is unsigned char but if you intend that to be a numeric value, I suggest you input to a temporary int variable, check its range, and then assign it to your array.

Also, please see Why is “while ( !feof (file) )” always wrong?.