2

I have a function called ADS_Transmit, declared as follows

void ADS_Transmit(uint8_t* data, uint16_t size);

The function is supposed to take arrays of uint8_t as parameter, but in some conditions, I'd like to send macros when calling the function. Like this

#define ADS_CMD_RST 0x06U
ADS_Transmit(ADS_CMD_RST, 1); // Does not work
uint8_t data = ADS_CMD_RST;
ADS_Transmit(&data, 1); // Works

Now perhaps obvious to many, sending the macro in the parameter doesn't work, since the function will use what's on register 0x06. But is there a way that I could send the macro in the parameter without changing the function?

Thank you in advance!

3
  • 1
    A macro represents a piece of text. A bunch of source code characters. Commented Jul 16, 2021 at 21:02
  • That's right. So I presume that it's not possible to do it in the way I want. I could just as well change the parameter of the function and work around it. Just wanted to make sure I understood why it doesnt work. Thanks! Commented Jul 16, 2021 at 21:04
  • No, it just doesn't make sense to talk about macros in this context. ADS_CMD_RST represents characters that make up the number 0x06U. So you should be asking "how do I send a number to a function that expect an address without storing the number in a variable and sending the address of the variable". You actually can do that, macros only muddle the water. Commented Jul 16, 2021 at 21:08

1 Answer 1

4

You can use a compound literal:

ADS_Transmit(&(uint8_t){ADS_CMD_RST}, 1);

This creates a temporary uint8_t variable, initialized with the value ADS_CMD_RST, and passes its address to ADS_Transmit.

Note that if ADS_Transmit modifies the memory pointed to by the first argument, you won't be able to access the updated values.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice! This is what I was looking for. My best effort was writing ADS_Transmit((uint8_t) &ADS_CMD_RST, 1) but that didn't work. Thank you for the help, this works!
Follow up question. If I'd like to use this method and send 2 bytes. Is there a nice way to do it? This is what I'm trying to do ADS_Transmit(&(uint8_t){(0x16U<<8)|(0x10U)}), though only the first 8 bits are being sent in this case
@Krippkrupp: Yep, use a compound literal of array type. ADS_Transmit((uint8_t[]){0x10U, 0x16U}, 2);
Now that I see it, perhaps it should've been obvious! This should also work for transmitting one byte as well, i.e ADS_Transmit((uint8_t[]){0x10U},1); Thanks a lot for taking your time to help me out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.