Electronics > Microcontrollers

Good old macros and GCC

(1/4) > >>

James:
I have been playing around with some macros in AVR studio and have hit a brick wall. From memory the statement

--- Code: ---#define     YOURCONSTANT        1
--- End code ---

assigns the value "1" to the constant "YOURCONSTANT".

How ever, is there a way to use a variable in place of the "1" and assign its value to the constant? The idea is to allow software control through a function for example

--- Code: --- something_init(unsigned char your_constant_value)
{
    #define     YOURCONSTANT          your_constant_value
    return YOURCONSTANT;
}
--- End code ---

Therefore if the value of your_constant_value is "hello" or "1", then the output of the function would be "hello" or "1".

So far my macro has taken the variable name as the input and therefore outputs the name of the variable not its value (e.g the output as "your_constant_value" not "hello" or "1").

It is propably just a misunderstanding of how macros work but if someone could please help that would be great.
Thanks,
James

Hideki:
Macros work by text substitution, so before the program is fed to the actual compiler, a preprocessor will replace all YOURCONSTANT with whatever text you define it to be.
So in your example, the line:

  return YOURCONSTANT;

will be changed into:

  return your_constant_value;

alm:
To add to what Hideki says, pre-processor directives like #define are, as the name suggests, interpreted by the pre-processor, which runs before the compiler. This pre-processor has no understanding of C, so putting a #define within a function doesn't make any difference. Most styles will only use #defines in header files to make them easier to find.

You appear to attempt to abuse pre-processor directives as global variables, this won't work. Any Using YOURCONSTANT outside the something_init() function will cause the compiler to complain about an undefined variable 'your_constant_value'.

Any proper C tutorial or textbook should explain how pre-processor directives work, and how they don't.

ruku:
Wikipedia has an excellent article on how things are handled in the C preprocessor.http://en.wikipedia.org/wiki/C_macro#Macro_definition_and_expansion

To the best of my knowledge, you can't change or re-assign the value of a constant. Otherwise, it wouldn't be... ...constant! :P

I'm not quite sure what you're trying to attempt, but there are a few things you can try... One thing you can do is macro expansion.


#define MAX(a, b) ((a > b) ? a : b)


Note the parentheses around the whole expression. This keeps the expression from being affected by order of operation.

You can also define a const variable, and allow the user (or coder) to set it once, but I'm not sure if that's what you're looking for. Global variables, perhaps?

Mechatrommer:
there's a proper way to do it, so use it!

--- Code: ---something_init(unsigned char your_constant_value)
{
    unsigned char *YOURCONSTANT = &your_constant_value;
    return YOURCONSTANT;
}

--- End code ---
put your runtime value in memory, not in #DEFINE, which stored compile time value/text.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod