I'm a little confused on if I can use #define to point to a function. I have a codec/DSP who's tool automatically generates pages of code like this:
SIGMA_WRITE_REGISTER(address, data, length);
SIGMA_WRITE_REGISTER(address, data, length);
SIGMA_WRITE_REGISTER(address, data, length);
....
Then in another .h file they do this:
#define SIGMA_WRITE_REGISTER( address, data, length ) {
/*TODO: implement macro or define as function*/}
Which helpfully doesn't define anything about writing registers. That's fine though wrote some code for my micro to write registers over I2C and that seems to be working. Now I don't want to just paste that code into the above define and have it instantiate it 1000 times. I was hoping I could just use the is define as an alias to my function?
Something like:
#define SIGMA_WRITE_REGISTER(address, data, length) { my_i2_c_func(address, data, length)}
I tried something like this and it compiled, I'm not so sure it's working though. Is this a valid thing to do or am I barking up the wrong tree?
The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.
The "using" statement allows you to specify multiple resources in a single statement. The object could also be created outside the "using" statement. The objects specified within the using block must implement the IDisposable interface.
IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on.
What is C# used for? Like other general-purpose programming languages, C# can be used to create a number of different programs and applications: mobile apps, desktop apps, cloud-based services, websites, enterprise software and games. Lots and lots of games.
Yes, you can surely use #define
to point to a full-fledged function or alias.
Consider a simpler example below, just for understanding
#define STRLEN(x) my_strlen(x)
and
int my_strlen(char *p)
{
// check for NULL pointer argument?
int x;
for (x = 0; *p++; x++);
return x;
}
now, in your code, you can use STRLEN
as you wish.
Note: Regarding the presence of { }
, you can either get rid of them, or use a do..while
loop, or define the function as a part of macro itself. Choice is yours. However, as MACRO is expanded during the pre-processing stage [resemble a textual replacement], you need to be extra bit careful about the {}
and the ;
usage. The MACRO usage should not break the code.
It's (almost) valid, but you need a semicolon after the last close parenthesis and before the close brace.
The braces in the replacement are completely superfluous, so you'll remove them anyway, and then you don't need the semicolon you just added. The braces give you a null statement after each statement block (and if you keep the semicolon in the macro, you also get a null statement after each macro invocation.
The comments in the .h
file indicate that you can replace the macro with a function (call). What you're doing is basically fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With