-14

Tell me how to create different strings in one pointer string like array. see following two program. 1st one give an errors. what is wrong here? Kindly correct it.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string *j={"nilesh",
                "rohit",
                "samir",};

    cout<<j<<endl;
}
#include <stdio.h>

const int MAX = 4;
int main ()
{
    char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};
    int i = 0;
    for ( i = 0; i < MAX; i++)
    {
        printf("Value of names[%d] = %s\n", i, names[i] );
    }
    return 0;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • No body will answer this nor will I...You should start reading...for your reference... http://ideone.com/Q7Zsme – HadeS Apr 10 '15 at 07:14
  • You should probably start [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 10 '15 at 09:24

2 Answers2

2

Write simply

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s[] = { "nilesh", "rohit", "samir", };

    for ( const string &t : s ) cout << t << endl;
}

Also instead of the array you could use standard class std::vector<std::string>

For example

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> v = { "nilesh", "rohit", "samir", };

    for ( const std::string &s : v ) std::cout << s << std::endl;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-3

Why not try it in this way ?

 #include <iostream>
 #include <string>
 using namespace std;

int main()
{
    string j[]={"nilesh",
                "rohit",
                "samir"};

    cout<<j<<endl;
}

Printing j directly wont print all the three names. You need to print j[0], j[1] ...

SDG99
  • 21
  • 7