I'm not that used to C++ and hoping that someone can help me out with the const keyword. If I make a parameter to a function 'const' ,and then try to reference it, like:

int some_func(const char* param){
  char* ptr = param;
  ...
}

to do something with it, the compiler gives the "invalid conversion from const char* to char*" when I try to pass in a parameter. The question is, is there some way to reference the 'param' while keeping it constant? or how can I fix the error message? Thanks in advance

you can do this: char* ptr = const_cast<char*>(param;) , but that defeats the purpose of using const in the first place. Casting out the const should be avoided whenever possible.

Either using :

const char* ptr = param ;
// or
char* ptr = const_cast<char*> (param) ;

But it would be interesting to know what you are trying to achieve here.....

Basically, I need to step through any array using pointers instead of the array indices. I changed my char* ptr to const char* ptr and it does work. Thanks.

Basically, I need to step through any array using pointers instead of the array indices. I changed my char* ptr to const char* ptr and it does work. Thanks.

But why would having a second const char* copy do you any good? Neither pointer can you modify, so copying it would be sort of pointless (sorry, bad pun).

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