•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 370,612 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,080 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1169 | Replies: 5
![]() |
•
•
Join Date: Jul 2005
Location: USA
Posts: 2
Reputation:
Rep Power: 0
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; ... }
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,104
Reputation:
Rep Power: 34
Solved Threads: 806
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 1:39 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
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."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
•
•
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). tuxation.com - Linux articles, tutorials, and discussions
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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



Linear Mode