vector of strings

hello,

i use the code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <stdlib.h>
#include <conio.h>

using namespace std;



int main (void)

{
    
    string *greet=new string[7];
    
    greet[0] = "Hello" ;
    greet[1] = "Hola" ;
    greet[2] = "Ciao" ;
    greet[3] = "Halo" ;
    gret[4] = "Bonjour" ;
    gret[5] = "Ola" ;
    gret[6] = "Hej" ;

    printf("%s",greet[0]);
    /*
    if( strcmp("Ciao",greet[1])== 0)
             printf("EQUAL\n\n");
    else
             printf("NOOOOOT EQUAL \n\n\n");
      */       
    

    system("PAUSE");
    return 0;
}


i cannot understand why the printf is not working. the message from the compiler is :

[Warning] cannot pass objects of non-POD type `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' through `...'; call will abort at runtime 
Last edited on
1
2
3
4
5
6
  
  greet[4] = "Bonjour" ;
  greet[5] = "Ola" ;
  greet[6] = "Hej" ;

  printf("%s",greet[0].c_str());


printf format specifier %s is looking for the plain old char * but string is a standard C++ class. So C++ provide the c_str() method for you to get back the plain old char *
A better question is "why are you using printf() in a C++ program with strings"? Use cout:

  25     cout << greet[ 0 ];

Hope this helps.
Thank you very much .
Topic archived. No new replies allowed.