If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: literal string

A const char* is a read-only string. The compiler can do things such as string pooling. For example, if you use the string literal "test" multiple times as in:

Code:

const char* theta ="test";
const char* beta = "test";

Then there will be only 1 string in memory with the contents "test". This is string pooling.
As such, you have to assign such a string literal to a const char* because it's not allowed to modify such a string.

On the other hand, when you assign a string literal such as "test" to a char[], then you are actually creating a copy of the string literal and storing it in your array. So, you are allowed to modify its contents because you have a copy.

Re: literal string

when you assign a string literal such as "test" to a char[], then you are actually creating a copy of the string literal and storing it in your array.

This one is the keyword, It's wonderful explanation.

So it's not supported this format? char* theta ="test"; but only const char* theta ="test"; ? I got only warning but I think it's preferable to use 2nd one.
I asked because my book says often uses this definition format.

Re: literal string

Older compilers never gave a warning when you used char*, maybe to not break legacy code. Anyway, modern compilers are starting to give a compilation warning. You are correct, it's just a warning, and not an error, but you really should start using const char* for string literals.

Re: literal string

It says: It can be used char* to define pointer which points to literal string but It cannot be used to change string.
Afterwars It says about constant array but it's another section, there is no specific reference among const char* and char*.
However I understood, thanks

2) is a way to use for string pooling as per above posts. However, this has the 'drawback' that s2 can be changed to point to another memory location so
3) is the suggestion as this is a const pointer to const data - ie neither the data nor the pointer contents can be changed.

4) is an array of char where the size of the array is determined at compile time as detailed above by Marc

5) is as 4) but it's contents can't be changed. The 'advantage' of this over 3) is that it is easy to obtain the number of chars in the array without iterating over the literal until a \0 is found. But note that a copy is still made - unlike 3).

Last edited by 2kaud; June 13th, 2018 at 05:07 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!

Re: literal string

This book was published in 2004 and uses c++98 (a very old standard). The current standard is c++17 and I would strongly suggest that you get a current c++ book that covers the c++17 standard.

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!

Re: literal string

Then it's about time they updated their course and teaching material recommendations. You are being taught c++ as it was in 1998 - not as it is now in 2018. There is a massive difference in current c++ as it is now to what it was then. Practices have changed considerably. You are not being taught how to write 'good' current c++ code. I would suggest you discuss this with the University. They are doing you a dis-service by not teaching you the current standard.

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!

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.