c - error: request for member xxxxxx in something

2019-09-11 15:22发布

问题:

This is in a program meant to work with ppm image files.

I'm getting a compilation error when trying to use a function that accepts a global struct variable and extracting that image's members.

This is the global struct (declared in ppmIO.c and ppmIO.h):

ppmIO.c:

struct Image *instance;

ppmIO.h:

struct Image
{
  int width;
  int height;
  unsigned char *data;
};

extern struct Image *instance;

This is how I call my function from main:

  ImageInvert(&instance);

These are the relevant parts of my imageManip.c file:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <ppmIO.h>
#include <imageManip.h>

void ImageInvert(struct Image **toInvert) {


  int i;
  int pix = (*toInvert->width) * (*toInvert->height);

  for (i = 0; i < pix; i++)
    {
      *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data));
      *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
      *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
    }

}

This is my imageManip.h file:

#include <ppmIO.h>

void ImageInvert(struct Image **toInvert);

void ImageSwap(struct Image **toSwap);

These are the errors I get:

imageManip.c:31:23: error: request for member ‘width’ in something not a structure or union
   int pix = (*toInvert->width) * (*toInvert->height);
                       ^
imageManip.c:31:44: error: request for member ‘height’ in something not a structure or union
   int pix = (*toInvert->width) * (*toInvert->height);
                                            ^
imageManip.c:35:18: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data));
                  ^
imageManip.c:35:60: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data));
                                                            ^
imageManip.c:35:67: error: expected ‘;’ before ‘)’ token
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data));
                                                                   ^
imageManip.c:35:67: error: expected statement before ‘)’ token
imageManip.c:36:18: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
                  ^
imageManip.c:36:60: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
                                                            ^
imageManip.c:36:69: error: expected ‘;’ before ‘)’ token
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
                                                                     ^
imageManip.c:36:69: error: expected statement before ‘)’ token
imageManip.c:37:18: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
                  ^
imageManip.c:37:60: error: request for member ‘data’ in something not a structure or union
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
                                                            ^
imageManip.c:37:69: error: expected ‘;’ before ‘)’ token
       *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));

Not sure if I'm accessing the members correctly or if I'm making the right use of pointers...

回答1:

There was an issue with how I was using brackets.

This is a working version of the imageInvert function:

void ImageInvert(struct Image **toInvert) {


  int i;
  int pix = (*toInvert)->width * (*toInvert)->height;

  for (i = 0; i < pix; i++)
    {
      (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data);
      (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data++);
      (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data++);
    }

}