DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C++ const help (http://www.daniweb.com/forums/thread69794.html)

blackjack Feb 12th, 2007 2:18 pm
C++ const help
 
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

iamthwee Feb 12th, 2007 2:37 pm
Re: C++ const help
 
Why not just use std::strings?

Ancient Dragon Feb 12th, 2007 2:38 pm
Re: C++ const help
 
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.

~s.o.s~ Feb 12th, 2007 2:39 pm
Re: C++ const help
 
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.....

blackjack Feb 12th, 2007 4:08 pm
Re: C++ const help
 
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.

John A Feb 12th, 2007 7:27 pm
Re: C++ const help
 
Quote:

Originally Posted by blackjack (Post 314344)
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).


All times are GMT -4. The time now is 12:05 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC