ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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)
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'.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.