C语言字符串与变量名的转换

#include <iostream>
#include <vector>
#include <string.h>
#include <queue>
#include<stdio.h>

using namespace std;

#define to_str(name)  (#name)

#define TIMx_ARR(n) TIM##n##_ARR

int main(void)
{
    freopen("G:\\data.txt", "r", stdin);

    int TIM1_ARR = 0;
    int TIM2_ARR = 0;


    TIMx_ARR(1) = 1;
    TIMx_ARR(2) = 2;

    cout << TIM1_ARR << endl;
    cout << TIM2_ARR << endl;
    cout << TIMx_ARR(2) << endl;

    for(int i = 1; i <= 2; i++)
    {
   //     TIMx_ARR(to_str(i)) = i*4;       error , to_str(i) can not replace before TIMx_ARR;
    }

    printf("%s", to_str(457779));

    return 0;
}

猜你喜欢

转载自blog.csdn.net/canhelove/article/details/89007164