c# - Using printf to print string value -


in c# can enumerate through array of strings, i'm have difficulty understanding why code below doesn't work in c++

int main( int argc, char ** argv ) {     string hi[] = { "hi", "cool", "what" };      (string s : hi)     {         printf("%s \n", s);     }      return 0; } 

i'm sure answer simple. help?

also, tried using instead, doesn't work either:

printf(s); 

oddly enough, array on integers work using %d. , yes have #include <string>

error info provided @chris (generated clang compiler):

main.cpp:12:25: error: cannot pass non-trivial object of type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') variadic function; expected type format string 'char *' [-wnon-pod-varargs]

  printf("%s \n", s);            ~~      ^ 

main.cpp:12:25: note: did mean call c_str() method?

  printf("%s \n", s);                    ^                     .c_str() 

use std::cout instead of printf.
problem passing std::string printf, when need const char*. using std::string::c_str solves this. better not use printf in first place.

live working example

#include <iostream> #include <string>  int main(int argc, char** argv) {     std::string hi[] = { "hi", "cool", "what" };      (const std::string& s : hi) {         std::cout << s << std::endl;     }      return 0; } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -