| | |
C++ const help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
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:
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
c++ Syntax (Toggle Plain Text)
int some_func(const char* param){ char* ptr = param; ... }
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. Last edited by Ancient Dragon; Feb 12th, 2007 at 2:39 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Either using :
But it would be interesting to know what you are trying to achieve here.....
c Syntax (Toggle Plain Text)
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.....
I don't accept change; I don't deserve to live.
•
•
•
•
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.
const char* copy do you any good? Neither pointer can you modify, so copying it would be sort of pointless (sorry, bad pun). "Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- #define or const? (C)
- A question about 'const' (C++)
- Error C2734: 'Value' : const obje (C++)
- AnsiString to Const Char* (C)
- Need help with Const, Ref and Classes. (C)
Other Threads in the C++ Forum
- Previous Thread: Searching array problems
- Next Thread: Problem using QT4 with codeblocks
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






