Lot Channel
Lot Channel

Reputation: 15

Conflicting types error for a function (C Programming)

C source file:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

#include "BmpProcessor.h"
#include "PixelProcessor.h"


/**
 * read BMP header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: Pointer to the destination BMP header
 */
void readBMPHeader(FILE* file, struct BMP_Header* header){
    fread(&header->sign_ID, sizeof(char)*2, 1, file);
    fread(&header->size, sizeof(int), 1, file);
    fread(&header->reserved1, sizeof(short), 1, file);
    fread(&header->reserved2, sizeof(short), 1, file);
    fread(&header->pix_Array, sizeof(int), 1, file);
}

/**
 * write BMP header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: The header made by makeBMPHeader function
 */
void writeBMPHeader(FILE* file, struct BMP_Header* header);

/**
 * read DIB header from a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: Pointer to the destination DIB header
 */
void readDIBHeader(FILE* file, struct DIB_Header* header){
    fread(&header->size, sizeof(struct DIB_Header), 1, file);
}

/**
 * write DIB header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: The header made by makeDIBHeader function
 */
void writeDIBHeader(FILE* file, struct DIB_Header* header);

/**
 * make BMP header based on width and height. 
 * The purpose of this is to create a new BMPHeader struct using the information 
 * from a PPMHeader when converting from PPM to BMP.
 *
 * @param  header: Pointer to the destination DIB header
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void makeBMPHeader(struct BMP_Header* header, int width, int height);


 /**
 * Makes new DIB header based on width and height. Useful for converting files from PPM to BMP.
 *
 * @param  header: Pointer to the destination DIB header
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void makeDIBHeader(struct DIB_Header* header, int width, int height);


/**
 * read Pixels from BMP file based on width and height.
 *
 * @param  file: A pointer to the file being read or written
 * @param  pArr: Pixel array of the image that this header is for
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height){
pArr = (struct pArr**)malloc(height*sizeof(void*));
for( int i = height-1; i = 0; i++){
    pArr[i] = (struct pArr*)malloc(width*sizeof(struct Pixel));
    fread(pArr[i],width,sizeof(struct Pixel),file);
    }
}

/**
 * write Pixels from BMP file based on width and height.
 *
 * @param  file: A pointer to the file being read or written
 * @param  pArr: Pixel array of the image that this header is for
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);

Header file:

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

struct Pixel{
    unsigned char red;
    unsigned char rgbgreen;
    unsigned char rgbblue;
};

Main c file:

#include "BmpProcessor.h"
#include "PixelProcessor.h"

struct BMP_Header *header;
struct DIB_Header *DIBheader;
struct Pixel **pArr;
FILE *file;

/*
 * 
 */
int main() {
    file = fopen("test2.bmp","rb");
    readBMPHeader(file, header);
    readDIBHeader(file, DIBheader);
    readPixelsBMP(file, pArr, DIBheader->img_width, DIBheader->img_height);
    fclose(file);
    return (EXIT_SUCCESS);
}

The error is shown for writePixelsBMP and readPixelsBMP only and I have tried declaring them at the start of the header file, main file, source file but nothing works. I am very confused, please help. Also, if you have time please check if my readPixelsBMP would do its job in reading pixels from a bmp file. Thank you.

Error message:

cd '/home/lot/NetBeansProjects/ser334'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/daryl/NetBeansProjects/ser334'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/ser334
make[2]: Entering directory '/home/daryl/NetBeansProjects/ser334'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/BmpProcessor.o.d"
gcc    -c -g -MMD -MP -MF "build/Debug/GNU-Linux/BmpProcessor.o.d" -o build/Debug/GNU-Linux/BmpProcessor.o BmpProcessor.c
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:93:39: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                       ^~~~~
BmpProcessor.h:104:40: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                        ^~~~~
BmpProcessor.c:81:6: error: conflicting types for ‘readPixelsBMP’
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height){
      ^~~~~~~~~~~~~
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:93:6: note: previous declaration of ‘readPixelsBMP’ was here
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~
BmpProcessor.c: In function ‘readPixelsBMP’:
BmpProcessor.c:82:6: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
 pArr = (struct pArr**)malloc(height*sizeof(void*));
      ^
BmpProcessor.c:84:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     pArr[i] = (struct pArr*)malloc(width*sizeof(struct Pixel));
             ^
BmpProcessor.c: At top level:
BmpProcessor.c:97:6: error: conflicting types for ‘writePixelsBMP’
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~~
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:104:6: note: previous declaration of ‘writePixelsBMP’ was here
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~~
nbproject/Makefile-Debug.mk:67: recipe for target 'build/Debug/GNU-Linux/BmpProcessor.o' failed
make[2]: *** [build/Debug/GNU-Linux/BmpProcessor.o] Error 1
make[2]: Leaving directory '/home/daryl/NetBeansProjects/ser334'
nbproject/Makefile-Debug.mk:60: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/daryl/NetBeansProjects/ser334'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

Upvotes: 1

Views: 729

Answers (1)

dbush
dbush

Reputation: 224167

When handling compiler errors, they should be addressed from the top down and include warnings. The first warning we see is:

BmpProcessor.h:93:39: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                       ^~~~~

This indicates that there's no definition for struct Pixel visible inside of BmpProcessor.h, resulting in struct Pixel being declared as part of the parameter list. In such a case, the scope of the declaration is only in the declaration itself.

Then later when the definition of this function is seen in BmpProcessor.c which does see the declaration of struct Pixel, you end up with two different struct declarations with the same name, and because the structs are different you get the error about conflicting types.

Since BmpProcessor.h depends on the struct definition in PixelProcessor.h, you need to #include "PixelProcessor.h" inside of BmpProcessor.h.

Upvotes: 1

Related Questions