void displayinfo(string &filename)
{
printf("%s% 38s\n", "Filename:", filename);
...
Warning: A non-POD object of type "std::string " passed as a variable argument to function "std::printf(const char*, ...)".
There is nothing online explaining what that warning means.
How would I get the printf to write this (assuming filename = test.txt):
Filename: (right justify filename) test.txt
Thanks in advance.
The explanation is quite simple: only PODs (Plain Old Data structures) can be passed as an argument to a variadic function (not a variadic function template though, just a simple variadic function using the ellipses).
std::string
is not a POD, but you can do:
printf("%s% 38s\n", "Filename:", filename.c_str());
// ^^^^^^^^
The c_str()
member function returns a const char*
to the encapsulated C string.
printf
, when used with the %s
format specifier, requires a pointer to char. You can get that from an std::string
via the c_str()
method:
printf("%s% 38s\n", "Filename:", filename.c_str());
As an aside, note that if you don't intend to modify or copy the input string, you should pass by const
reference:
void displayinfo(const string& filename) { .... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With