c-格式“%d”期望类型为'int *'的参数,但参数3的类型为int [英] c- format "%d" expects argument of type 'int *', but argument 3 has type int

查看:1291
本文介绍了c-格式“%d”期望类型为'int *'的参数,但参数3的类型为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程的新手,我需要从两个.dat文件中获取图像,并比较两者是否相等。文件首先包含图像数量,然后是一些图像。如果所有图像都相同,我需要返回1.

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.

其中一个.dat文件的图片:

Pic of one of the .dat files:

F1.dat

前20个意味着20个组的图像。
那么3 4将意味着3x4矩阵,其余的将是矩阵。与其他19个矩阵相同。

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.

这是我写的代码:

#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]));


推荐答案

fscanf 将值读入提供的地址。警告是你的朋友。您正在收到该特定警告,因为编译器告诉您它会将 i1.im [i] [j] 的值解释为地址并尝试取消引用它为了将扫描值放在里面。可能的结果是段错误。

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.

请尝试:

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

这篇关于c-格式“%d”期望类型为'int *'的参数,但参数3的类型为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆