Thread: Preprozessor question

  1. #1
    Registered User
    Join Date
    Dec 2010

    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


    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

    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
    Long Beach, CA
    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Join Date
    Aug 2010
    Ontario Canada
    First... why would you not simply use the defines you show in your header...

    // instead of...
    x = getsensaddr(5);
    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...
    // 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