简体   繁体   English

带有预处理指令的指针

[英]Pointers with preprocessor directives

Working on a code I've seen this: 在编写代码时,我已经看到了:

#define X(i)        ((array[i]).x[0])
#define Y(i)        ((array[i]).y[0])

typedef struct {
  int x[2];
  int y[2];
} coord_t;

coord_t* array = malloc(sizeof(coord_t)* 10);
int myX = X(5);

This makes the access to array almost hardcoded with X and Y preprocessor directives. 这使得对array的访问几乎用XY预处理程序指令进行了硬编码。 What I would like to do is to pass the pointer to the preprocessor directive, something like: 我想做的是将指针传递给预处理器指令,例如:

#define X(myarray,i) myarray[i].x[0]

where myarray could be any coord_t pointer: myarray可以是任何coord_t指针:

coord_t* a = malloc(sizeof(coord_t)* 4);
coord_t* b = malloc(sizeof(coord_t)* 12);

int myX = X(a,1);
int myotherX = X(b,6);

I've done this, and it compiles without any errors however I am not getting the same results. 我已经做到了,它编译没有任何错误,但是我没有得到相同的结果。 My question is, can I #define X in this way? 我的问题是,我可以#define X这样?

#define X(myarray,i) myarray[i].x[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM