Preprozessor question

This is a discussion on Preprozessor question within the C Programming forums, part of the General Programming Boards category; Hi Experts! I have a quiestion... 1. in my header-file there is: #define SENSOR_0 0xe0 #define SENSOR_1 0xe2 #define SENSOR_2 ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Preprozessor question

    Hi Experts!
    I have a quiestion...

    1. in my header-file there is:

    #define SENSOR_0 0xe0
    #define SENSOR_1 0xe2
    #define SENSOR_2 0xe4
    #define SENSOR_3 0xe6
    #define SENSOR_4 0xe8
    #define SENSOR_5 0xea
    #define SENSOR_6 0xec

    with this macro-function:

    #define getSensAdr(NR) SENS_ADR_##NR

    if i write this code on my c-file

    getSensAdr(5)

    i get the right value (0xea)

    The problem is if i use a variable to get the address of the define like this:

    int i=5
    getSensAdr(5)

    then i get this error message (by the way i'm using gcc as my compiler)
    ../sensor.c:46: error: 'SENS_ADR_i' undeclared (first use in this function)

    do you know if there is any way to solve this problem?

    sorry about my english

    thank u very much!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Nope, can't be done. There is no way for the preprocessor (which happens even before compilation or linking, let alone running) to know the value of a variable at run time. Furthermore, this wouldn't even work if you defined i as some const int, for which the value was known and fixed at compile time. The preprocessor basically does text substitution and concatenation for #defines and macros.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    A few admin issues:
    • Do not cross post. You also posted this thread in the C++ programming forum, but I closed that one. Is this supposed to be C or C++?
    • Post your code in bbcode tags.
    • Post correct examples. The SENS_ADR_##NR looks like it should be SENSOR_##NR, or perhaps all the SENSOR_0 etc should be SENS_ADR_0 etc; I am guessing that the second getSensAdr(5) should actually be getSensAdr(i).


    Now, if my guess is correct, then the reason is simple: the macro getSensAdr(i) is subtituted with SENS_ADR_i, regardless of the value of i, because you are dealing with the text substitution of the preprocessor.

    The way I see it is that you should just use an array instead of a bunch of macros.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First... why would you not simply use the defines you show in your header...

    Code:
    // instead of...
    x = getsensaddr(5);
    
    //use...
    x = SENSOR_5;
    I have a pretty strong notion that's why the defines are there...

    If you need those addresses in variables create an array...
    Code:
    // add to header
    const int Sensor[6] = {SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, SENSOR_5, SENSOR_6};
    
    // use as...
    int i = 4;
    x = Sensor[i];

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21