Can't pass an array of string into a function using pointer.

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

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
#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;
 }
Is this What you're looking for?

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
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

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


int main()
{
    int SIZE; 
    
    cout << "How big do you want your array to be? ";
    cin >> SIZE;
    
    string s[SIZE];

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

    print(s, SIZE);

    return 0;
 }
Last edited on
Topic archived. No new replies allowed.