LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2006, 06:01 AM   #16
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138

Actually with const char* it means the pointer variable is constant, not the contents of the location pointed to by the pointer.

a is a pointer in this example

Pointer Variable Block of memory
[a]---------------------> [] [] [] [] [] [] [] [] [] [] - Starting@2000001 (say)

Now what const char* a means is that a can only point to that memory location (because it's declared a const) and not any other memory location.

If I'm wrong, somebody please correct me, but that is my understanding of pointers.

As I said before, it's all very confusing so why not just use std::string? These trivial issues are not worth bothering about when you're interesting more in the bigger picture of your program.
 
Old 08-05-2006, 06:45 AM   #17
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
...
a is a pointer in this example

Pointer Variable Block of memory
[a]---------------------> [] [] [] [] [] [] [] [] [] [] - Starting@2000001 (say)

Now what const char* a means is that a can only point to that memory location (because it's declared a const) and not any other memory location.

If I'm wrong, somebody please correct me, but that is my understanding of pointers.
...
That is not correct Harishankar.
The const keyword applies to the object/type which is to the left handside of the keyword, if there is nothing to the left then it applies to the right.

char const* ptr: ptr is a pointer to a constant char
const char* ptr: ptr is a pointer to a constant char

char * const ptr: ptr is a constant pointer to a char


char const * const ptr : ptr is a constant pointer to a constant char.
const char * const ptr: ptr is a constant pointer to a constant char.


The easiest way to read is from right to left and put the const to the right of what it effects(in my opinion)

edit:
http://www.parashift.com/c++-faq-lit...rrectness.html

Last edited by dmail; 08-05-2006 at 07:19 AM. Reason: added to link to const reference
 
Old 08-05-2006, 06:56 AM   #18
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Thank you very much for the clarification. Much googling didn't help me out there.
 
Old 08-05-2006, 07:15 AM   #19
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
Originally Posted by Harishankar
As I said before, it's all very confusing so why not just use std::string? These trivial issues are not worth bothering about when you're interesting more in the bigger picture of your program.
Oh, I agree, that 'std::string' is very often nicer than a char[], but that has very little to do with the "const issue" at all. A very common case, when 'const' is advisable is, if you want to pass an object to a function by reference (to avoid the expensive call of a copy ctor), but you want also to assure that the original object doesn't change during process; example:
Code:
void f(const std::string &str) //or similar: void f(const std::string *str)
{
  //...use 'str' but don't change it
}
This is a very fundamental thing and I found that the keyword const is one of the things many people uses without thinking about; they declare 'const' here and 'const' there and if needed "const_cast" it back. This is not what 'const' was made for. If I see a function that takes a const parameter, then I trust, that this parameter don't change during process. And if I assume, that I will change a variable later, then I don't modify it with 'const'.

Last edited by Flesym; 08-05-2006 at 07:31 AM.
 
Old 08-05-2006, 07:19 AM   #20
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
OK i was wrong. Thanks for the clarification.
 
Old 08-05-2006, 07:38 AM   #21
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
I am still confused.


if:

char *x ="ABC";
const char *y = "DEF";

can I

A: x=y; ?
B: y=x; ?

for me A would be ok but not B. Am I rigth?
 
Old 08-05-2006, 07:48 AM   #22
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Code:
A: x=y; ?
B: y=x; ?

for me A would be ok but not B. Am I rigth?
The other way round! You can always (and only) assign a non-const to const. In other words: You can always restrict, but not widen the modification.

Last edited by Flesym; 08-05-2006 at 07:49 AM.
 
Old 08-05-2006, 10:49 AM   #23
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Thanks.

I draw it up on a paper and now I get it. dmail had the explaination. I though "const char* ptr" was a constant pointer to a char but its a pointer to a constant char.

I still learn something every day.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error: invalid conversion from `void*' to `char**', where should I start to look for RHLinuxGUY Programming 4 05-20-2006 08:53 PM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
Conversion from char[100] to char* ? zahadumy Programming 2 12-11-2005 09:04 PM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration