Passing a pointer to an array of strings in to a function

Dear,

I have check some post but I can not see what is happening to me. I am experimenting with pointers, arrays and functions to learn how to use it. I have initialized a simple pointer to an string array using dynamic allocation (*p_names)(to further expand the array later), I want to pass that pointer (*p_names)as an argument for the function IntrNewName, but I am not able to do it. What is happening? How I can pass a pointer to an array of string (for example cointaining different names )into a function? When I change the type to char instead of string it just works fine (see second program after % row) of course with a single char.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>

void IntrNewName(int *p_UsersNumber,int* p_Names);//Or it should be: void IntrNewName(int *p_UsersNumber,string* p_Names);    ?????'

using namespace std;

int main()
{
    int UsersNumber=1;
    int *p_UsersNumber=&UsersNumber;
    string *p_Names=new string [UsersNumber];
    IntrNewName(p_UsersNumber,p_Names);
    //print to see the result of IntrNewName
    cout<<*p_UsersNumber;
    cout<<p_Names[0];

}

void IntrNewName(int *p_UsersNumber,int* p_Names)
{
    cout<<"Please introduce new name:"<<endl;
    cin>>p_Names[*p_UsersNumber-1];
    *p_UsersNumber+=1;
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

USING char

#include <iostream>
#include <string>

void IntrNewName(int *p_UsersNumber,char* p_Names);

using namespace std;

int main()
{
    int UsersNumber=1;
    int *p_UsersNumber=&UsersNumber;
    char *p_Names=new char [UsersNumber];
    IntrNewName(p_UsersNumber,p_Names);
    //print to see the result of IntrNewName
    cout<<*p_UsersNumber;
    cout<<p_Names[0];

}

void IntrNewName(int *p_UsersNumber,char* p_Names)
{
    cout<<"Please introduce new name:"<<endl;
    cin>>p_Names[*p_UsersNumber-1];
    *p_UsersNumber+=1;
}

Last edited on
The problem is that what you want is a string array, but in your second example you use a char array, which is similar to a single string. Also, it seems unnecessary to pass UsersNumber using a pointer; a reference should be sufficient.

 
//Or it should be: void IntrNewName(int *p_UsersNumber,string* p_Names);    ?????' 


Yes.

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
#include <iostream>
#include <string>

using namespace std;

void IntrNewName(int &usersNumber, string *p_Names);

int main()
{
    int usersNumber = 1;
    string *p_Names = new string[usersNumber];
    IntrNewName(usersNumber, p_Names);
    //print to see the result of IntrNewName
    cout<<usersNumber;
    cout<<p_Names[0];

}

void IntrNewName(int &usersNumber, string *p_Names)
{
    cout << "Please introduce new name: ";
    cin >> p_Names[usersNumber - 1];
    cout << endl;
    usersNumber += 1;
}
I haven't seen where this is going yet, but please remember to be careful when you're working with your array. If you don't remember to resize it, you might run into trouble.
Thank you for your replies.

Finally I have figured out what happened when I was checking the DivineLight code. The function prototype should be after "using namespace std;" to correctly compile it. I had seen this error yesterday a hundred times. The new code is, using pointers:

#include <iostream>
#include <string>

using namespace std;

void IntrNewName(int *p_UsersNumber, string *p_Names);

int main()
{
int UsersNumber=1;
int *p_UsersNumber=&UsersNumber;
string *p_Names=new string [UsersNumber];
IntrNewName(p_UsersNumber,p_Names);
//print to see the result of IntrNewName
cout<<UsersNumber;
cout<<p_Names[0];

}

void IntrNewName(int *p_UsersNumber,string *p_Names)
{
cout<<"Please introduce new name:"<<endl;
cin>>p_Names[*p_UsersNumber-1];
*p_UsersNumber+=1;
}


At this point I am trying to learn programing in C++ by my own. The piece of code where I had the doubt it was the starting point of some program to learn how to use pointers in dynamic memory allocation but I got stuck at the point where I had to pass the pointer to string array to the function.

Usually at every step I take I try to see the result to know if the code works up to that point.

Now I can continue with my program. Every time you introduce a new name, the array will increase their size dynamically to allocate the new name. It sounds not very sophisticated but my objective now is understand what I am doing. If I have to say the truth I do not see a clear difference between pointers and references yet!

Thank you again for your help!
Topic archived. No new replies allowed.