4 ms·
I am at a loss to understand this. I learned C in 1989 and sprintf takes a minimum of two arguments, but typically takes three or more, the first one being the
by anonymousiam 2y ago
I am at a loss to understand this. I learned C in 1989 and sprintf takes a minimum of two arguments, but typically takes three or more, the first one being the string for the output to be written to. Perhaps the author was writing about printf, which takes a minimum of one argument, but typically takes two or more?
- jznsis 2y agoThis is just a choice of having to provide an output buffer to the function instead of returning a new buffer since it's easier to inject custom allocator behaviour that way
- anonymousiam 2y agoCan you elaborate please? Your description does not match what I see. My understanding is that this is a way to more strongly type function arguments, as an alternative to va_arg.
- jznsis 2y agoYes, but the final result is out_str, not a forward to console, isn't it? Either way, the format string discussion for the printf familiy of functions is very similar between them
- anonymousiam 2y agoOkay, got it. So you are agreeing with me that the author should have used printf instead of sprintf. Thanks!
- deleted 2y ago[deleted]
- jznsis 2y agoNo
- jwilk 2y agoThere's no return value here: void my_sprintf(char* format, ???) { sprintf(format, ???); }
- hoping1 2y agoAuthor here, thanks for pointing that out! It was a mistake on my part (: I originally wrote this about printf, but decided I should include the section where I implement it, and I decided I didn't want to say anything about side effects, so I changed everything to a version that just returns the new string. In my head this is called "sprintf" (and the notes I based this on, linked in the post, did the same thing) though I can see now how this was confusing to people. Especially since I didn't rewrite it perfectly!
- trealira 2y agoYes, I was also confused at first. The closest C function to this one is asprintf, because this function gives you back a new, separate string, which you may or may not decide to print. Just imagine if asprintf simply returned a char* pointing to an allocation by malloc, instead of taking an extra char ** as an out parameter. char *my_sprintf(const char *, ...);
- hoping1 2y agoAuthor here. I originally wrote this about printf, but changed it because I wanted to stay away from any discussion of side effects. The rewrite wasn't perfect! In my head, a printf that returns the new string instead of printing it is called "sprintf," and I know others have this impression in a culture that is more general than just C. But you're absolutely right!