Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 734 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#define a tuple in C

#1
I want to be able to define a tuple which represents the arguments needed by other macros.

I think the best way to show what I want is to show an example:

#include <avr/io.h>

#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED_PIN PB7
#define LED LED_PORT, LED_DDR, LED_PIN

#define OUTPUT(port, ddr, pin) ddr |= 1 << pin

void main(void) {
OUTPUT(LED);
}

I want `OUTPUT(LED)` to be then expanded into:

LED_DDR |= 1 << LED_PIN

The problem that I get is to do with the order of expansion, and results in the following error:

> macro "OUTPUT" requires 3 arguments, but only 1 given

This is for use with an AVR project with custom built hardware where I have defined `LED` and other components with a respective `LED_PORT` `LED_DDR` and `LED_PIN`.

I then want to define more macros that can take this LED and use the appropriate arguments to map to the most succinct way possible.

Is this possible with the standard C-preprocessor?
Reply

#2
You can add a level of indirection to the macro to achieve this:

#define OUTPUT_I(port, ddr, pin) ddr |= 1 << pin
#define OUTPUT(spec) OUTPUT_I(spec)

During rescanning, `spec` is expanded before `OUTPUT_I`, so the `OUTPUT_I` macro sees three parameters.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through