tags:

views:

131

answers:

2

Hi all.. 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?

+1  A: 

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

William Billingsley
@william: Ya u r right.. my actual question is, how can i de-reference to get the value 0x03.. i dont understand why (pid_buff_ptr[0] + 1) is dereferencing 0x03, instead of memory location containing 0x03.. my understanding is, pid_buff_ptr[0] is the first element in the array, which is a pointer to the buffer (0x01, 0x03)
inquisitive
A: 

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?

Alok
Hey thanks a lot Alok!! i have tried your code and it works.. btw, when i try to have declaration like "int pid_buff_ptr[][] = {LOOK_UP}", i am not able to do so.. what is my understanding is, you need not mention the column subscript of a 2D array, when u try initializing it in declaration itself.. am i right.. i need the above because, FILL_BUFF_N may be dynamic and need not contain only 2 elements
inquisitive
That's correct. You can have only one dimension "free". If you need more than two FILL_BUFF_* macros, you can easily do this by changing int pid_table[2] = {LOOK_UP}; to int pid_table[] = {LOOK_UP};, adding the required FILL_BUFF_* macros, and changing LOOK_UP macro.But I still don't know what you're trying to do. There is almost certainly an easier way to do it.
Alok
@Alok: well its actually to save RAM space and avoid having const variables..
inquisitive