The usage of pre-compile instruction ##

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值