0

I have a struct that is throwing "dereferencing pointer to incomplete type" errors when I try to access its member functions. I didn't create the struct, my professor did, so I am reasonably sure that when accessed properly it will work perfectly. This is the code:

Pixel.h (Created by professor):

#ifndef PIXEL_H
#define PIXEL_H

#include <stdbool.h>
#include <stdio.h>

#define BYTES_PER_PIXEL 3

typedef struct
{
        unsigned char R;
        unsigned char G;
        unsigned char B;
}Pixel;

Pixel.c (Also created by professor):

#include "Pixel.h"

bool readFromFile(FILE* inputFile, Pixel* newPxl)
{
        // Irrelevant code
        return false;
}

bmpFilter.c (main file) (Created by me):

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

int main(int argc, char* argv[])
{
        // Irrelevant code, inputFile initialized
        struct Pixel* pix;
        pix = malloc(sizeof(pix));

        pix->readFromFile(inputFile, pix);
}

Error:

bmpFilter.c: In function ‘main’:
bmpFilter.c:33:5: error: dereferencing pointer to incomplete type ‘struct Pixel’
  pix->readFromFile(inputFile, pix);

The error is almost certainly in bmpFilter and I have been unable to find how to fix it thus far.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

1

You didn't declare struct Pixel. You declared an anonymous struct, and typedefed it as Pixel alone. Either change your declaration of the struct to:

typedef struct Pixel // <-- Added name for struct
{
        unsigned char R;
        unsigned char G;
        unsigned char B;
}Pixel;

or when you use it, just declare the variable as:

Pixel* pix;

Given the definition of Pixel comes from your professor (and you're probably not allowed to change it), I'd suggest just changing struct Pixel* pix; to Pixel* pix; to match the provided code.

Your other problem is that you do pix->readFromFile(inputFile, pix);; pix doesn't contain a function pointer named readFromFile (and even if it did, mallocing the struct means the pointer would be garbage). You meant to call the global function:

readFromFile(inputFile, pix);  // No pix-> needed
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271