I want to do something like this

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
  MyFunc("test");
  return 0;
}

void MyFunc(const char* Filename)
{
  cout << strcat(Filename, ".tst") << endl;
}

but it tells me that my const char* cannot be used where it is expecting char*

Is the solution simply to accept it as a char* instead of a const char*? I thought it was always better to have things const if you weren't going to modify them?

Thanks

Dave

Well strcat does modify the string, and as written your code would almost certainly segfault if allowed to run.

Stripping out (or avoiding) const because it's inconvenient is the wrong way to go.

ok, so assuming I pass the function a const char*, I'd need to convert it to a char* before I append anything. Do I do this?

char* newthign = const_cast<char*>(oldthing);

where newthing is now a char* and oldthing is a const char*?

>Is the solution simply to accept it as a char* instead of a const char*?
Hardly. It's better to understand the error and correct your logic rather than bumble your way through a workaround that will end up mysteriously crashing your program.

>I thought it was always better to have things const if you weren't going to modify them?
Not only is it better to use const when you aren't planning on modifying something, it's better to use const everywhere and only remove it when you absolutely have to. That guideline is much more of a pain to adhere to, but the const correctness you get from it is worth the effort.

>ok, so assuming I pass the function a const char*
Assuming you want to support passing string constants, your best bet is to convert to a string object and use the concatenation operator:

#include <string>

void MyFunc(const char* Filename)
{
  cout << std::string ( Filename ) + ".tst" << endl;
}

>I'd need to convert it to a char* before I append anything.
No, because the pointer points to read-only memory. It really is const, so tossing away the const qualification is only going to move your error from compile-time to runtime. Your code is broken, and playing around with const isn't going to change that.

This question has already been answered. Start a new discussion instead.