-
June 30th, 2018, 02:55 AM
#1
[RESOLVED] C++ constant pointer
Code:
void fun(char *s); // It doesn't change string
int main() {
char const hello[] = “test“;
fun(hello) // error!!!!!
// cannot convert parameter 1 from 'const char [5]' to 'char *‘
fun(const_cast<char *>(hello)); // OK
return 0; }
The definition char const hello[] = “ciao“ creates constant pointer named hello, which will contain memory address which will point to memory area containing test.
It's OK?
I understood array name is always declared as constant pointer so why It's necessary to explicit it by char const hello[]?
The keyword const permit, as in pointer case, to not change pointer value? But if It's just constant for definition ?!?!
-
June 30th, 2018, 03:22 AM
#2
Re: C++ constant pointer
When dealing with pointers, there are two types of const to be considered. Consider
where a is a pointer to char. There is the pointer variable itself (a) and the memory to which a points. When defined like this, both the contents of a (the pointer) and the memory contents can be changed.
If we define like this
then a itself is a const - ie its value can't be changed to another memory address but the contents of the memory to which it points can.
But if we define like this
Code:
const char * a;
// or char const * a;
then the contents of a can be changed (a can point to another memory location) but the contents of memory to which a points cannot be changed.
These can be combined so
Code:
const char * const a;
// or char const * const a;
means a constant pointer to constant memory.
When defining an array
Code:
char hello[] = "test";
this is effectively the same type as
Code:
char * const hello;
ie a constant pointer to memory that can be changed.
But if you define as
Code:
const char hello[] = "test";
then this is effectively the same type as
Code:
const char * const hello;
// or char const * const hello;
ie neither the contents of the variable nor the contents to which it points can be changed.
I understood array name is always declared as constant pointer so why It's necessary to explicit it by char const hello[]?
From the above name is declared as constant pointer but not constant memory data. The const here is that the memory is now also const and can't be changed.
Last edited by 2kaud; June 30th, 2018 at 03:33 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2018, 03:43 AM
#3
Re: C++ constant pointer
Originally Posted by 2kaud
When dealing with pointers, there are two types of const to be considered.
I studied and understood right difference among constant pointer to memory that can be changed and pointer to constant.
I explained wrongly.
My doubt is about array , example:
Code:
char const hello[] = “test“;
char norm[] = “test“;
norm is a identifier which represents address (eg XXXXX) of first element of array
I know norm cannot be changed because is costant pointer , It points to XXXX address (first element of memory area for array), right?
So I cannot assign norm another address, right? I can play with pointer arithmetics or vector index.
But if It's true what I'm saying what's purpose to define char const norm[] = “test“; ?
-
June 30th, 2018, 03:52 AM
#4
Re: C++ constant pointer
what's purpose to define char const norm[] = “test“; ?
as I explained in post #2, so that the contents of norm (ie "test") cannot be changed as well.
If you have
Code:
char norm[] = "test";
then
is valid and will change the contents of memory to "best". However if you have
Code:
char const norm[] = "test";
then
will produce a compilation error as the memory pointed to by norm now also can't be changed.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2018, 04:32 AM
#5
Re: C++ constant pointer
Code:
void fun(char *s); // It doesn't change string
Note that if fun() doesn't change the string, then it should be defined as
Code:
void fun(const char *s); // It doesn't change string
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2018, 04:37 AM
#6
Re: C++ constant pointer
Originally Posted by 2kaud
as I explained in post #2, so that the contents of norm (ie "test") cannot be changed as well.
Code:
char const norm[] = "test";
then
will produce a compilation error as the memory pointed to by norm now also can't be changed.
But It's different compared to pointers case, because char* const first permits to not change memory address which is contained in first. If first=0x45663df I cannot change this value inside first.
For array it's another meaning.??!
-
June 30th, 2018, 05:15 AM
#7
Re: C++ constant pointer
Yes, but you can change the contents of memory pointed to by first.
If first = 0x45663df then char * const does not permit the value of first to change, but it does permit the contents of memory pointed to by first to change. So if the memory starting at 0x45663df contains hello\0 then
is allowed and the contents of memory at 0x45663df now contains zello\0
but if const char * const is used, the you are not allowed to change the contents of the memory pointed to by first.
You need to understand the 2 concepts
- allowed/not allowed to change the contents of the pointer variable (eg change first from 0x45663df to say 0x45775ae) the const goes after the * working left to right
- allowed/not allowed to change the contents of the memory pointed to by the variable (eg change the contents of memory at 0x45663df) the const goes before the * working left to right
These 2 concepts are seperate. Either or both or non can be used.
when defining an array, the default is not allow the value of the variable to change but do allow the contents pointed to by the variable to change. If you don't want the contents of the array to be allowed to change, then specify const.
For more info, see http://www.learncpp.com/cpp-tutorial...ers-and-const/
Last edited by 2kaud; June 30th, 2018 at 05:18 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2018, 06:54 AM
#8
Re: C++ constant pointer
Originally Posted by 2kaud
Yes, but you can change the contents of memory pointed to by first.
If first = 0x45663df then char * const does not permit the value of first to change, but it does permit the contents of memory pointed to by first to change.
Dear 2kaud,
I understood both concepts and I'm not contesting you that it does permit the contents of memory pointed to by first to change.
In this moment I'd to like to concentrate only to one of 2 concepts: allowed/not allowed to change the contents of the pointer variable when subject is array.
when defining an array, the default is not allow the value of the variable to change but do allow the contents pointed to by the variable to change. If you don't want the contents of the array to be allowed to change, then specify const.
It's very interesting! Those one you wrote (which I underline in blue) it's default way, be careful! I'm referencing to: not allow the value of the variable to change. Now I ask myself why I have to define char const hello[] = “test“; (my initial question) if default is just set?!! That is, hello is not allowed the value of the its variable to be changed. I'm referencing to memory address. Const, in this way, is too excessive !?!? This one is my doubt.
Thanks, I'll read as soon as possible.
Last edited by zio_mangrovia; June 30th, 2018 at 06:57 AM.
-
June 30th, 2018, 07:34 AM
#9
Re: C++ constant pointer
Now I ask myself why I have to define char const hello[] = “test“; (my initial question) if default is just set?!! That is, hello is not allowed the value of the its variable to be changed. I'm referencing to memory address. Const, in this way, is too excessive !?!? This one is my doubt.
No, you are not understanding. if I have
Code:
char hello[] = "test";
then
is allowed but
is not allowed (const pointer - contents of variable hello cannot be changed).
However if I have
Code:
const char hello[] = "test";
then
is now not allowed (as const memory data and const pointer) and
is still not allowed (const pointer).
The difference between
Code:
char hello[] = "test";
and
Code:
const char hello[] = "test";
is whether or nor the data "test" can or cannot be changed.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 1st, 2018, 03:55 AM
#10
Re: C++ constant pointer
You are very explicit (I just understood your examples) but It's missing this case:
char hello[] = "test" vs char const hello[] = "test"
Code:
char hello[] = "test";
++hello;
is still not allowed (const pointer).
you said:hello is a const pointer.
When you write the attribute const to the right of <type> you are defining constant pointer... so why add const again in
"char const hello[]"
according to my opinion It's not necessary to write const on the right of char because hello is just itself a constant pointer, dod you understand what I want to say?
-
July 1st, 2018, 04:00 AM
#11
Re: C++ constant pointer
but It's missing this case:
char hello[] = "test" vs char const hello[] = "test"
No it isn't. My comments in previous posts to this thread explicitly cover this crucial big difference. Try compiling this code
Code:
char test1[] = "test1";
char const test2[] = "test2";
test1[0] = 'z';
test2[0] = 'z';
what happens?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 1st, 2018, 04:22 AM
#12
Re: C++ constant pointer
error: assignment of read-only location 'test2[0]'
so the following statements are equivalent?
char const test2[] = "test2";
const char test2[] = "test2";
-
July 1st, 2018, 04:47 AM
#13
Re: C++ constant pointer
so the following statements are equivalent?
char const test2[] = "test2";
const char test2[] = "test2";
Yes. The const may be either to the left or right of the type (as per my post #2) with the same effect. Note that if * is used, this is not considered part of the type.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 1st, 2018, 05:06 AM
#14
Re: C++ constant pointer
Excuse me 2kaud, you are very patient and kind, so I can add:
The const may be either to the left or right of the type (as per my post #2) with the same effect.
It's a particular case for array because when you have <type> *, you have 2 different concepts which you described deeply in previous threads. Right?
-
July 1st, 2018, 05:25 AM
#15
Re: C++ constant pointer
Originally Posted by zio_mangrovia
Excuse me 2kaud, you are very patient and kind, so I can add:
It's a particular case for array because when you have <type> *, you have 2 different concepts which you described deeply in previous threads. Right?
Yes.
For an array you have
Code:
char a1[] = "test";
const char a2[] = "test";
char const a3[] = "test";
where the definition of a2 and a3 means the same.
for a pointer you have:
Code:
char * p1;
char * const p2;
char const * p3;
const char * p4;
char const * const p5;
const char * const p6;
Now see if you can explain the differences between a1, a2, a3, p1, p2, p3, p4, p5, p6
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|