I need to pass const char *
to a function. Before I pass it, I usually need to build it from some variables. I can never decide which approach is more elegant, and overall better:
- Allocate an array long enough to fit the text and use
sprintf
to build the final variable and pass it to the function. - Initialize string
s
with a variable using the + operator (concatenation) and then pass it to the function usings.c_str()
.
Cons of using an array: May not fit the entire text.
Pro: Fast.
Cons of using a string: I don't need to worry about memory management, easy to build. Pro: Slow.
My second question is: How do you usually build a complete string from other variable strings?