-1

Don't know if this is possible. I've been searching for it but nothing comes up.

I'd like to have a function that merges multiple values and returns them as a string literal (so no malloc and subsequent free). This could be achieved by printing to the return value (if that makes sense and is possible), or maybe using fprintf with a literal as destination, instead of say stdout, and then that literal could be returned.

The parameters of the function below are just an example, there could be other types such as long, etc.

#include <stdio.h>

char *format_and_return_literal(const char *s, int n, char c)
{
        int m = 7;

        //(1):  Possible to fprintf to return value of function?
        //fprintf(<func-return-literal>, "hello %s: %d %d %c", s, m, n, c);
        //return <func-return-literal>;

        // or

        //(2):  Possible to return string literal made of above parameters?
        //return "hello %s: %d %d %c", s, m, n, c)
}

int main()
{
        printf("%s\n", format_and_return_literal("foo", 33, 'M'));
        return 0;
}

Update: Thank you all. Perhaps the terminology I used wasn't the most correct, but I achieved what I wanted following @Davislor's suggestion of using a static buffer. I'm aware of snprintf but thank you anyway, @PeterJ.

char *format_and_return_literal(const char *s, int n, char c)
{
        static char buf[1024];
        int m = 7;

        snprintf(buf, sizeof(buf), "hello %s: %d %d %c", s, m, n, c);

        return buf;
}
Daniel
  • 2,380
  • 29
  • 44
  • 3
    String literals are fixed at compile-time, what you ask is impossible. – M.M Jul 12 '17 at 23:41
  • "returns them as a string literal (so no malloc and subsequent free)" - that's not how it works. You can't retroactively preallocate these strings. You need to allocate them dynamically, and you need to free them dynamically. – user2357112 Jul 12 '17 at 23:42
  • Your options include: memory allocation; using preallocated fixed-sized buffers; or also passing into the function enough information for the function to output the result to the device – M.M Jul 12 '17 at 23:42
  • "Stack buffers" are also possible but risky, in case the input happens to cause a stack overflow – M.M Jul 12 '17 at 23:43
  • He says "literal" but probably he thinks "string" – 0___________ Jul 12 '17 at 23:56
  • @PeterJ: I think literal would be correct. I'd like to send stuff into the function, parse it as I see fit, then return the result as a string literal which doesn't need to be freed. Davislor suggests the use of a static buffer, so that would not be a literal but should do what I want (essentially not having to worry about freeing memory). – Daniel Jul 13 '17 at 00:03
  • 1
    Not literal. Literal is something fixed for example "5&_&_3&_43". You can't change it as it is an UB – 0___________ Jul 13 '17 at 00:06

2 Answers2

1

You can snprintf to a fixed-size, static buffer and return that. Subsequent calls would then overwrite the previous return value. Alternatively, pass in the destination buffer, owned by the caller. as an argument.

Davislor
  • 14,674
  • 2
  • 34
  • 49
1

Abstracting from logic of such a solution you have version of the printf which "prints" to the given buffer it is called sprintf and another snprintf which is a bit more safe

0___________
  • 60,014
  • 4
  • 34
  • 74