0

I try to outsource the input buffers of my snprintf() function:

So how to translate this working example:

struct ElementStruct {
    char element1[10];
    char element2[10];
}elementStruct;

char rxTxBuffer[30] = {0};

snprintf(rxTxBuffer, 30, "{ %s %s }", elementStruct.element1, elementStruct.element2);

To this not working example:

struct ElementStruct {
    char element1[10];
    char element2[10];
}elementStruct;

char rxTxBuffer[30] = {0};

char string[] = {"{ %s %s }"};
const char* restrict elements[] = {elementStruct.element1, elementStruct.element2};

snprintf(rxTxBuffer, 30 ,string, *elements);
9
  • C doesn't allow argument spreading from an array. The closest you can get is vsnprintf(), but it use a va_list rather than an array; it's useful for writing your own printf-like functions that get variable numbers of arguments. Commented Jan 25, 2019 at 21:29
  • See also stackoverflow.com/q/54366443/1848654. Commented Jan 25, 2019 at 21:31
  • Thanks for your hints, but I'm searching for a solution in C Commented Jan 25, 2019 at 21:37
  • ... er, yes? What did you think we were talking about? Commented Jan 25, 2019 at 21:42
  • Sorry, I thought vsnprintf() is only included in C++. Commented Jan 25, 2019 at 21:49

0

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.