views:

168

answers:

2

You can see what i'm trying to do below:

typedef struct image_bounds {
    int xmin, ymin, xmax, ymax;
} image_bounds;

#define IMAGE_BOUNDS(X) ((image_bounds *)(X));

typedef struct {
    image_bounds bounds;
    float dummy;
} demo;

int
main(void) {
    demo my_image;

    /* this works fine */
    ((image_bounds *)(&my_image))->xmin = 10;

    /* why doesn't this work? i get the following error:
    /* In function main:
      cast.c:20: error: expected expression before = token
    */    
    IMAGE_BOUNDS(&my_image)->xmin = 20;

    return 0;
}

As you can see from above the C cast works but the macro version does not, what am I doing wrong?

+14  A: 

You need to lose the semicolon from the definition of IMAGE_BOUNDS:

#define IMAGE_BOUNDS(X) ((image_bounds *)(X))
RichieHindle
hahahah thanks! :)
banister
You can't call yourself a C programmer if you haven't developed a blind spot for semicolons!
Mark Ransom
+2  A: 

In the version without macros you have ->xmin before the =, in the one with macros you don't.

sepp2k
yeah sorry that was just a typo :(
banister