Here's a dumb question for you

What's the point of "const char * whatever;"

Or the point of char *'s at all really. Isn't a char just a pointer anyway, to the beginning of what may end up being an array?
The 'point' of pointers is to hold a memory address (ie. to point somewhere.)

The 'point' of const char* whatever is to define a variable named whatever that can point to memory containing an object of type const char.

No, a char is not a pointer. Yes, pointers may point to the first element of an array.
a char is a character, like A or @
so are const char *'s and char *'s the same then? why do some need to be const and others not?
const is a promise to the compiler that you will not try to change the value using this label. You do it to protect yourself from your own mistakes.

For example:
1
2
char* x = "BeansOnToast";
x[0] = 'Z';

If you try to change any of the letters in that, you're asking for trouble, as it's forbidden (see "string literal"). A segFault is probably what you'll get when you run it (and when you compiled it, your compiler should have warned you).

1
2
const char* x= "BeansOnToast";
x[0] = 'Z';

This simply won't compile, so you've made it impossible for yourself to cause that segFault.



Last edited on
So is char* x like an array then? but... without bounds? like if I did

1
2
3
4
5
char* x = "BeansOnToast";
x+13 = 'Z';
//or
x[13]='Z';


would that be legal? I clearly didn't learn pointers as well as I thought I did
Last edited on
Also, I'm going to crush your curiosity right here and now:
1
2
3
const char *s = "c-string";
char *evil = (char*)s; //evil!
evil[0] = 'Z'; //evil! 
While you can do this, this is considered as breaking the promise you made to the compiler. 'const' is a compile-time safety, but it allows the compiler to make assumptions and do things that can behave differently than non-const things.
It would be legal if you used
1
2
3
char x[] = "BeansOnToast"; //Initialize x and memcpy "BeansOnToast"
x+13 = 'Z'; // Valid, but Out Of Bounds (Error writing to address error)
x[13] = 'Z'; // Same 

instead, because you declared a new variable.
But we are talking about taking a constant's pointer.
1
2
3
4
5
char * x = "BeansOnToast"; // "BeansOnToast" is constant, and it should not be edited. So you should use 'const char *' to be sure it will never be edited.
x+13 = 'Z'; // SegFault / Out Of Bounds
x[13] = 'Z'; // Same thing.
x+1 = 'Z'; // SegFault
x[1] = 'Z'; // Same 
Last edited on
x+13 = 'Z';
1. "x+13" returns a temporary pointer which points to a location 13 units away from x. You cannot assign to temporaries.
2. If you could assign to temporaries this code would do nothing, because you're making that temporary pointer point to something at memory address unsigned int('Z')...then doing nothing with it.
Last edited on
1
2
3
const char *s = "c-string";
char *evil = (char*)s; //evil!
evil[0] = 'Z'; //evil!  


A good reason to prefer C++ style casts, where you can't accidentally throw away const.

x+13 = 'Z';
1. "x+13" returns a temporary pointer which points to a location 13 units away from x. You cannot assign to temporaries.
2. If you could assign to temporaries this code would do nothing, because you're making that temporary pointer point to something at memory address unsigned int('Z')...then doing nothing with it.

Sure? I think it could be true if it was like this:
((&x)+13) = 'Z'
As x is a char*, it points to the first char, and adding 13 you get a pointer to the 13th char (not existing), but shouldn't it be valid? (I know what you mean)
x is a char *
&x is a temporary char **
you add 13 to it
then try to assign a character to a temporary char**
...I fail to see logic ;p
Last edited on
What you just said is this code: ((&x)+13) = 'Z' (assign a character to a char **) (which is wrong)
What i am explaining you is this code: x+13 = 'Z' (assign a character to a char *) (which i think is good)
I am probably failing to express myself in the right way...
Or maybe i'm completely wrong.
EDIT: Or maybe it should be like: *(x+13) = 'Z'; Probably my fault then, sorry, LOL.
Last edited on
Yes, "*(x+13)" is a reference to a char, and you can easily assign to that. That doesn't mean it won't segfault as discussed above, but it's legal syntax ;)
Exactly what i meant, so it was my fault, sorry ;D
Sorry to resurrect an old thread, was rereading this and realized I also have no clue what a char ** is... what is that?
It is a (pointer to a (pointer to a (char)))
So it follows the same rules as a pointer then? such as

char A='A';
char* memaddress=A;
char** blah=memaddress;

almost. You forgot the ampersands:

1
2
3
4
5
char A='A';
char* memaddress=&A;
char** blah=&memaddress;

cout << **blah;  // prints 'A' 
Last edited on
Got it, thank you very much!
Topic archived. No new replies allowed.