I've written the following program..but it doesn't work. Why?

It shows an error: cannot convert ‘std::string* {aka std::basic_string<char>*}’ to ‘std::string** {aka std::basic_string<char>**}’ for argument ‘1’ to ‘void print(std::string**)’|

#include <bits/stdc++.h>

using namespace std;

void print(string *ar[])
{
    for(int i=0; i<5; i++)
    {
        cout<<*ar[i]<<endl;
    }
}


int main()
{
    string s[5];

    for(int i=0; i<5; i++)
    {
        getline(cin,s[i]);
    }

    print(&s);

    return 0;
 }

line 5: void print(string *ar[]) which becomes
line 5: void print(string **)

line 23 print(&s) which becomes
line 23 print(s)

Since the name of an array is the address of the array. In your code if you put before line 23

cout << s;
cout << &s;

You should get the same value. If you want to get an array in your print function I would just write:

void print(const string * arr, int size)
{
    for (int i = 0; i < size; i++)
        cout << arr[i];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.