Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a function that takes "char *filename" as argument

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

I have a function that takes this as argument:

char *filename

It is a pointer to char array, I guess. I can use this function by specifying directly the filename argument. Something like this:

myFunction("filname.txt");

But I cannot do that because the filename is loaded by the application and I store it in a string inside a vector. So when I would like to use I should do it like this:

myFunction(myVector[0]);

but this will give a me a compilation error saying that myFunction doesn't have overloaded method for this argument.

Now: myVector[0] is a string, exactly like "filename.txt". So why it doesn't work? Does someone can enlight?

thanks
 
Well C neither has vectors, nor overloaded methods.

Perhaps you're compiling this as C++, and what you have is something like
Code:
std::vector< std::string > myVector;

If this is so, then perhaps this will work for you.
Code:
myFunction( myVector[0].c_str() );


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
But then you'll also have to cast away the const, since your function isn't const correct.
i.e.
Code:
myFunction( const_cast<char*>(myVector[0].c_str()) );

Although, in the unlikely chance that your function does actually modify the filename, you should copy it to a non-const array first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top