c - error: request for member xxxxxx in something not a structure or union

孤人 提交于 2019-12-25 08:49:58

问题


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++);
    }

}


来源:https://stackoverflow.com/questions/40078453/c-error-request-for-member-xxxxxx-in-something-not-a-structure-or-union

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!