| | |
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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






