0

Possible Duplicate:
How can I pass a vector variable to a function?

I have following code:

std::vector <const char*> value;
value[0] = buf.c_str();

// Now I have to pass the vector variable to function.
// Function declaration: int my_function(const char *)
my_function(value[0]);

However, I'm getting this error:

error: cannot convert 'std::vector<char*, std::allocator<char*> >' to 'const char*' for argument '1' to 'int my_function(const void*)'

Can you please help me resolve this problem?

Community
  • 1
  • 1
Balaji
  • 21
  • 1
  • 3
  • Same User Duplicate : http://stackoverflow.com/questions/4895644/how-can-i-pass-a-vector-variable-to-a-function –  Feb 04 '11 at 08:15
  • The error you're getting suggests that the function you're calling doesn't match the description in your comments. Can you confirm that the provided sample code matches what you have? The code you've posted is syntactically valid. – templatetypedef Feb 04 '11 at 08:16
  • 1
    Edit your existing question, don't ask a whole new one. – GManNickG Feb 04 '11 at 08:21

3 Answers3

1

something like:

typedef std::vector<std::string> myvec;

void func(myvec &vec){

 //use vec
}


main.cpp:

myvec vec;

func(vec);
programmersbook
  • 3,914
  • 1
  • 20
  • 13
0

Your function is defined as taking a const void * parameter, not a const char * parameter (according to the compiler's error message). Change both the declaration and definition and you should be fine, or you could cast the value to const void *.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

The only safe way to do this is to recieve a std::vector in your function. On most compilers though you can send it as a char array by

$(vec[0])

As I said, this is not safe, since the standard don't say that a vector need to be in consecutive memory, but then again, mot compilers put a vector in consecutive memory.

martiert
  • 1,636
  • 2
  • 18
  • 23