If you want to replace part string with your macro parameter, you can use pre-compile instruction ##.
If there is no token (eg, . -> ) before the replace macro parameter, you need place ## before your macro parameter.
If there is no token (eg, . -> ) after the replace macro parameter, you need place ## after your macro parameter.
The test codes
==============
int test_pre_process1()
{
typedef struct
{
uint32_t spi4_tx_cnt;
uint32_t spi4_rx_cnt;
uint32_t rgmii_tx_cnt;
uint32_t rgmii_rx_cnt;
} fpga_regs;
#define dump_cnt(regs, part, dir) regs.part##_##dir##_cnt
#define dump_cntp(regsp, part, dir) regsp->part##_##dir##_cnt
fpga_regs my_fpga;
fpga_regs *my_fpgap = &my_fpga;
my_fpga.spi4_tx_cnt = 1;
my_fpga.spi4_rx_cnt = 2;
my_fpga.rgmii_tx_cnt = 3;
my_fpga.rgmii_rx_cnt = 4;
printf("%d\n", dump_cnt(my_fpga, spi4, tx));
printf("%d\n", dump_cnt(my_fpga, spi4, rx));
printf("%d\n", dump_cnt(my_fpga, rgmii, tx));
printf("%d\n", dump_cnt(my_fpga, rgmii, rx));
printf("%d\n", dump_cntp(my_fpgap, spi4, tx));
printf("%d\n", dump_cntp(my_fpgap, spi4, rx));
printf("%d\n", dump_cntp(my_fpgap, rgmii, tx));
printf("%d\n", dump_cntp(my_fpgap, rgmii, rx));
return 0;
}
int test_pre_process2()
{
#define A(x) T_##x
#define B(x) T##x##__
int A(1) = 10; //equal to int T_1 = 10;
int B(2) = 20; //equal to int T2__ = 20;
printf("\n");
printf("T_1: %d\n", T_1);
printf("T2__: %d\n", T2__);
return 0;
}
int main (int argc, char *argv[])
{
printf("====== Hello, Vim! ======\n\n");
test_pre_process1();
test_pre_process2();
printf("\n");
/*system("PAUSE");*/
return 0;
}
The test result
===============
====== Hello, Vim! ======
1
2
3
4
1
2
3
4
T_1: 10
T2__: 20
If there is no token (eg, . -> ) before the replace macro parameter, you need place ## before your macro parameter.
If there is no token (eg, . -> ) after the replace macro parameter, you need place ## after your macro parameter.
The test codes
==============
int test_pre_process1()
{
typedef struct
{
uint32_t spi4_tx_cnt;
uint32_t spi4_rx_cnt;
uint32_t rgmii_tx_cnt;
uint32_t rgmii_rx_cnt;
} fpga_regs;
#define dump_cnt(regs, part, dir) regs.part##_##dir##_cnt
#define dump_cntp(regsp, part, dir) regsp->part##_##dir##_cnt
fpga_regs my_fpga;
fpga_regs *my_fpgap = &my_fpga;
my_fpga.spi4_tx_cnt = 1;
my_fpga.spi4_rx_cnt = 2;
my_fpga.rgmii_tx_cnt = 3;
my_fpga.rgmii_rx_cnt = 4;
printf("%d\n", dump_cnt(my_fpga, spi4, tx));
printf("%d\n", dump_cnt(my_fpga, spi4, rx));
printf("%d\n", dump_cnt(my_fpga, rgmii, tx));
printf("%d\n", dump_cnt(my_fpga, rgmii, rx));
printf("%d\n", dump_cntp(my_fpgap, spi4, tx));
printf("%d\n", dump_cntp(my_fpgap, spi4, rx));
printf("%d\n", dump_cntp(my_fpgap, rgmii, tx));
printf("%d\n", dump_cntp(my_fpgap, rgmii, rx));
return 0;
}
int test_pre_process2()
{
#define A(x) T_##x
#define B(x) T##x##__
int A(1) = 10; //equal to int T_1 = 10;
int B(2) = 20; //equal to int T2__ = 20;
printf("\n");
printf("T_1: %d\n", T_1);
printf("T2__: %d\n", T2__);
return 0;
}
int main (int argc, char *argv[])
{
printf("====== Hello, Vim! ======\n\n");
test_pre_process1();
test_pre_process2();
printf("\n");
/*system("PAUSE");*/
return 0;
}
The test result
===============
====== Hello, Vim! ======
1
2
3
4
1
2
3
4
T_1: 10
T2__: 20