Passing string array to function

Hi, I have some problems with passing an array of string type to a function. I
want to store some names in the array which I then want to return back to the function. See the code below (the interesting parts are line 1 and 6)


void linklist::mother_colors(std::string *F_colors[]){ ////THIS ONE
node *q;
int i;
for( q=p, i = 0; q!= NULL; q=q->next, i++){
if(q->x.get_age >= 2 && q->x.get_sex == 'F'){
F_colors[i] = q->x.get_color(); ////AND THIS ONE
}
}
}

Answers very apprecitated
closed account (zb0S216C)
dolle39 wrote:
std::string *F_colors[]

This is equivalent to: std::string **F_colors. Secretly, a pair of brackets in a parameter declaration indicates a pointer. So effectively, you're creating a pointer to a pointer. As as a result, you're trying to assign whatever is returned from x.get_color() to a pointer. There's a few ways to fix this:

1) Remove the asterisk proceeding F_colors and dereference F_colors once: *F_colors[i] = ...
2) Dereference F_colors twice: **F_colors[i] = ...

Also, please, use code-tags; look for the button to the right of the edit box([<>]).

Wazzak
Last edited on
Thanks for reply but it still does not work. I tried to do as you told in (1) but without luck. Im getting the following
complilation errors:

g++ -g -Wno-write-strings src/bunny.cpp src/game.cpp src/linklist.cpp src/test.cpp -o my_program

src/linklist.cpp: In member function ‘void linklist::mother_colors(std::string*)’:
src/linklist.cpp:274:24: error: invalid use of member (did you forget the ‘&’ ?)
src/linklist.cpp:274:45: error: invalid use of member (did you forget the ‘&’ ?)
make: *** [my_program] Fel 1

any suggestions?
Topic archived. No new replies allowed.