inquisitive
inquisitive

Reputation: 1680

Regarding macros

may i know what is the problem in using the below x-macro code

#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?

Upvotes: 0

Views: 212

Answers (2)

Alok Singhal
Alok Singhal

Reputation: 96211

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.

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().
  • main() returns int.
  • Since main() returns int, you should return an int from it. Traditionally, 0 means success.
  • I don't know what _TCHAR is, but if it is not an alias or #define for char, you might be in trouble.

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?

Upvotes: 0

William Billingsley
William Billingsley

Reputation: 771

(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?

pid_buff_ptr[0] == 0x02

so

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

so

*(pid_buff_ptr[0] + 1)

is dereferencing 0x03 as a pointer.

Upvotes: 1

Related Questions