×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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

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

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.

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close