简体   繁体   English

关于宏

[英]Regarding macros

may i know what is the problem in using the below x-macro code 我可以知道使用下面的x宏代码有什么问题吗

#define FILL_BUFF_1 (int *)(0x01, 0x02)
#define FILL_BUFF_2 (int *)(0x03, 0x04)

#define X(a,b)

#define LOOK_UP \
     X(0x13, FILL_BUFF_1), \
     X(0x14, FILL_BUFF_2)

#undef X

#define X(a,b) a
int pid_table[2] = {LOOK_UP};
#undef X

#define X(a,b) b

int *pid_buff_ptr[2] = {LOOK_UP};

void main(int argc, _TCHAR* argv[])
{
    printf("%d ", (pid_buff_ptr+0));  // displays 0x02

    printf("%d ", (pid_buff_ptr+1));  // displays 0x04

    printf("%d " ,*(pid_buff_ptr[0] + 1)); // doesnt work
}

How can I make the above code to access other elements in the buffer? 如何使上面的代码访问缓冲区中的其他元素?

(Taking a wild stab at this just because it's been sitting here a while unanswered.) (对此采取狂野的攻击只是因为它已经坐在这里好一阵子了,没有人回答。)

In the last line, you appear to be dereferencing the value 0x03 as a pointer -- I suspect you are getting a SIGSEGV? 在最后一行中,您似乎将值0x03取消引用作为指针-我怀疑您正在获取SIGSEGV?

pid_buff_ptr[0] == 0x02

so 所以

(pid_buff_ptr[0] + 1) == 0x03

so 所以

*(pid_buff_ptr[0] + 1)

is dereferencing 0x03 as a pointer. 正在取消引用0x03作为指针。

I am not sure what exactly you're trying to do, but if I guessed correctly, you want to have the pid_buff_ptr variable contain an array of int values, not an array of int * values. 我不确定您到底想做什么,但是如果我猜对了,您想让pid_buff_ptr变量包含一个int值数组,而不是一个int *值数组。

If that is the case, you need to change 如果是这种情况,您需要进行更改

#define FILL_BUFF_1 (int *)(0x01, 0x02)
#define FILL_BUFF_2 (int *)(0x03, 0x04)

to

#define FILL_BUFF_1 {0x01, 0x02}
#define FILL_BUFF_2 {0x03, 0x04}

Change the following: 更改以下内容:

int *pid_buff_ptr[2] = {LOOK_UP};

to

int pid_buff_ptr[][2] = {LOOK_UP};

To print, you would use something like: 要进行打印,您将使用类似:

printf("%d ", pid_buff_ptr[0][1]);

Of course, I could be wrong. 当然,我可能是错的。

Now, some other comments: 现在,其他一些评论:

  • You need to #include <stdio.h> before using printf() . 使用printf()之前,您需要#include <stdio.h>
  • main() returns int . main()返回int
  • Since main() returns int , you should return an int from it. 由于main()返回int ,因此您应该从中返回一个int Traditionally, 0 means success. 传统上, 0表示成功。
  • I don't know what _TCHAR is, but if it is not an alias or #define for char , you might be in trouble. 我不知道_TCHAR是什么,但是如果它不是char的别名或#define ,则可能会遇到麻烦。

Even with the above changes, I don't understand the need to do the trickery with the pre-processor. 即使进行了上述更改,我也不了解使用预处理器进行欺骗的必要性。 What exactly are you trying to do? 您到底想做什么?

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

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