hor*_*guy 1 c macros casting c-preprocessor
你可以看到我在下面尝试做的事情:
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;
}
Run Code Online (Sandbox Code Playgroud)
正如你从上面可以看到,C演员有效,但宏版本没有,我做错了什么?
Ric*_*dle 15
您需要从IMAGE_BOUNDS的定义中丢失分号:
#define IMAGE_BOUNDS(X) ((image_bounds *)(X))
Run Code Online (Sandbox Code Playgroud)