473,473 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

difference between between these "char"s

what is the difference between these 2:

char name = "hackers";
char* name = "hackers";

Mar 4 '07 #1
34 2648
char name = "hackers";

i do not know anything about this.
char* name = "hackers";
i guess, this will become an array of "char"s and name will point to
1st element of the array .i.e.

name and name[0] will mean same

right ?
Mar 4 '07 #2
On Mar 4, 2:38 pm, "arnuld" <geek.arn...@gmail.comwrote:
char name = "hackers";

i do not know anything about this.
char* name = "hackers";

i guess, this will become an array of "char"s and name will point to
1st element of the array .i.e.

name and name[0] will mean same

right ?
I dont this think that
char name = "hackers"; statement is a valid one.

char name means a variable of type char which has one byte in size.

Regards,
Sarath

Mar 4 '07 #3
In article <11**********************@t69g2000cwt.googlegroups .com>,
ge*********@gmail.com says...
what is the difference between these 2:

char name = "hackers";
char* name = "hackers";
The main difference is that the second will compile, but the first one
won't.

Perhaps you intended:

char name[] = "hackers";
vs.
char *name = "hackers";

?

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 4 '07 #4
On Mar 4, 10:54 am, Jerry Coffin <jcof...@taeus.comwrote:
char name = "hackers";
char* name = "hackers";

The main difference is that the second will compile, but the first one
won't.
that i did not know. may i know why ?
Perhaps you intended:

char name[] = "hackers";
vs.
char *name = "hackers";

?
what's the difference between these 2 ?
--
Later,
Jerry.

The universe is a figment of its own imagination.

Mar 4 '07 #5
arnuld wrote:
>>On Mar 4, 10:54 am, Jerry Coffin <jcof...@taeus.comwrote:
>>>char name = "hackers";
char* name = "hackers";

The main difference is that the second will compile, but the first one
won't.

that i did not know. may i know why ?
A char can only be initialised with a character constant, not a string
literal. A string literal is a const char*.
>
>>Perhaps you intended:

char name[] = "hackers";
vs.
char *name = "hackers";

?

what's the difference between these 2 ?
The former initialises an array of char with
{'h','a','c','k','e','r','s','\0'} the latter initialises a char* with
the address of the string literal "hackers" in this case, the compiler
may issue a diagnostic due the the type of a string literal being const
char*

--
Ian Collins.
Mar 4 '07 #6
On Mar 4, 11:45 am, Ian Collins <ian-n...@hotmail.comwrote:
>char name[] = "hackers";
vs.
char *name = "hackers";
>?
what's the difference between these 2 ?

The former initialises an array of char with
{'h','a','c','k','e','r','s','\0'} the latter initialises a char* with
the address of the string literal "hackers" in this case, the compiler
may issue a diagnostic due the the type of a string literal being const
char*
it means we can there are 2 ways to create a string:

an array and
a char*

in both cases we will get a string, so it doe snot matter we use which
one.

right ?

Mar 4 '07 #7
arnuld wrote:
>
it means we can there are 2 ways to create a string:

an array and
a char*

in both cases we will get a string, so it doe snot matter we use which
one.

right ?
Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.

--
Ian Collins.
Mar 4 '07 #8
On Mar 4, 12:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
arnuld wrote:
in both cases we will get a string, so it doe snot matter we use which
one.
right ?
Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.
ok, i new that.
char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";
that is really strange for me as i did not know. i collect 2 points
here:

1.) we can use /char*/ where we are damn sure that we need a constant
string.

2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not be
modified.

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.
whwn will my kitchen explode ?

;-)

--
Ian Collins.

Mar 4 '07 #9
arnuld wrote:
>
1.) we can use /char*/ where we are damn sure that we need a constant
string.
const char*.

--
Ian Collins.
Mar 4 '07 #10
arnuld wrote:
>char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

that is really strange for me as i did not know. i collect 2 points
here:

1.) we can use /char*/ where we are damn sure that we need a constant
string.
Yes, however, this is only true if you directly initialize it with a string
literal. That's a stupid special rule to make C++ more compatible with C
code written by sloppy programmers.
2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not be
modified.
A string literal is also an array of char.

Mar 4 '07 #11
On Mar 4, 3:00 pm, Rolf Magnus <ramag...@t-online.dewrote:
1.) we can use /char*/ where we are damn sure that we need a constant
string.

Yes, however, this is only true if you directly initialize it with a string
literal. That's a stupid special rule to make C++ more compatible with C
code written by sloppy programmers.
:-(

i really hate that decision of Stroustrup.
2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not be
modified.

A string literal is also an array of char.
the difference is a "string literal", is a /const/ array of char

?

Mar 4 '07 #12
arnuld wrote:
2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not be
modified.

A string literal is also an array of char.

the difference is a "string literal", is a /const/ array of char

?
I'd call it an array of const char, but yes. Also, no matter where it's
defined in the source code, it has static storage duration, i.e. exists
throughout the whole execution time of the program.

Mar 4 '07 #13
On Mar 4, 3:15 pm, Rolf Magnus <ramag...@t-online.dewrote:
arnuld wrote:
the difference is a "string literal", is a /const/ array of char
?

I'd call it an array of const char, but yes. Also, no matter where it's
defined in the source code, it has static storage duration, i.e. exists
throughout the whole execution time of the program.
thanks, that was new thing you told, static allocation of string
literal.

now i can say i know about char*

:-)

Mar 4 '07 #14
On 4 Mar, 10:36, "arnuld" <geek.arn...@gmail.comwrote:
now i can say i know about char*
Don't forget that C++ gives you std::string to remove the need to
worry about these complications. That's not to say you shouldn't
understand raw char arrays too - you certainly should - but now you've
learnt how the hard way works don't forget you have the easy way
available to you too.

Gavin Deane

Mar 4 '07 #15
arnuld wrote:
>On Mar 4, 11:45 am, Ian Collins <ian-n...@hotmail.comwrote:
>>>char name[] = "hackers";
vs.
char *name = "hackers";
>>>?
>>what's the difference between these 2 ?

The former initialises an array of char with
{'h','a','c','k','e','r','s','\0'} the latter initialises a char*
with the address of the string literal "hackers" in this case, the
compiler may issue a diagnostic due the the type of a string literal
being const char*

it means we can there are 2 ways to create a string:

an array and
a char*

in both cases we will get a string, so it doe snot matter we use which
one.

right ?
No, not quite right.

Have you read chapter 5 yet? :-)
Bo Persson
Mar 4 '07 #16
arnuld wrote:
>On Mar 4, 3:00 pm, Rolf Magnus <ramag...@t-online.dewrote:
>>1.) we can use /char*/ where we are damn sure that we need a
constant string.

Yes, however, this is only true if you directly initialize it with a
string literal. That's a stupid special rule to make C++ more
compatible with C code written by sloppy programmers.

:-(

i really hate that decision of Stroustrup.
It wasn't for him to decide, it is inherited from the C language.

At the time when C++ was designed, C didn't have the keyword const, so all
string literals were of the type char*. That made code like

char* x = "Hello";

valid. In C++ string literals have the type 'const char*', but this old C
code has got a special dispensation so that it will continue to work anyway.
Everywhere else it is an error to assign a pointer to const to a pointer to
non-const.

Also, even though x is of type char*, in C++ it is still illegal to assign
to *x. The string is a constant!
>
>>2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not
be modified.

A string literal is also an array of char.

the difference is a "string literal", is a /const/ array of char

?
At this time it could be a good idea to take a peak at chapter 20 about
std::string, which solves these kinds of problems the C++ way. In C++ you
hardly ever have to use char* directly.

You have now found exactly the reason why many in this group recommended
"Accelerated C++", which introduces std::string on the first page of chapter
1. The subtle difficulties of pointers and arrays are postponed to chapter
10, long after teaching about the simpler things like classes, templates and
overloaded functions.
Bo Persson
Mar 4 '07 #17
On Mar 4, 4:51 pm, "Bo Persson" <b...@gmb.dkwrote:
arnuld wrote:
i really hate that decision of Stroustrup.

It wasn't for him to decide, it is inherited from the C language.
that was what i said. his decision for "keepiing backward
compatibility with C" was a bad decision. we got a complex beast in
the end, named C++.

but please leave it here. i do not want it to become the topic of
discussion. i want the topic to be "chat and char*", so i will not
talk about any OT things. my mistake.

At the time when C++ was designed, C didn't have the keyword const, so all
string literals were of the type char*. That made code like

char* x = "Hello";

valid. In C++ string literals have the type 'const char*', but this old C
code has got a special dispensation so that it will continue to work anyway.
Everywhere else it is an error to assign a pointer to const to a pointer to
non-const.
what does last sentence mean:

"a pointer to const to a pointer to non-const"
this pointer to a char notation:

char* x; or
char *x;

how will you define your last sentence in this notation?
Also, even though x is of type char*, in C++ it is still illegal to assign
to *x. The string is a constant!
did not get it. IMVHO, please explain using an example.
>2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not
be modified.
A string literal is also an array of char.
the difference is a "string literal", is a /const/ array of char
?

At this time it could be a good idea to take a peak at chapter 20 about
std::string, which solves these kinds of problems the C++ way.
i know about "std::strings" and understand how they work. i only want
to understand the "char and char*" for now.

In C++ you hardly ever have to use char* directly.
YES, you use "std::string". i am saying, it is important to understand
"char and char*" when you have understood "std::string".

You have now found exactly the reason why many in this group recommended
"Accelerated C++", which introduces std::string on the first page of chapter
1. The subtle difficulties of pointers and arrays are postponed to chapter
10, long after teaching about the simpler things like classes, templates and
overloaded functions.
to me, i do not think pointers are difficult than Templates. i do not
have any teacher/mentor who can teach me, except you folks. all of the
colleges here teach a language named VC++ & they do not agree that
"Linux is a good OS" and i am a UNIX man, that is the trouble.
Bo Persson

Mar 4 '07 #18
On Mar 4, 12:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
arnuld wrote:
Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.

a "const char*" can easily be modified, see:
#include <iostream>

int main() {
int x = 7;
const char* author = "Stroustrup";

std::cout << x
<< "\t"
<< author
<<"\n";

x = 100;
author = "Bjarne";

std::cout << x
<< "\t"
<< author
<<"\n";
}

---------- OUTPUT
[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra char-po.cpp
[arch@voodo cpp]$ ./a.out
7 Stroustrup
100 Bjarne
[arch@voodo cpp]$
------------------------

no undefined behaviour, i modified the "const char*". now, If i can
modify "const", what is the significance of "const" then ? [1]
-- arnuld
http://arnuld.blogspot.com

[1] but a "const int/char/float" can not be modified. i have checked
that.

Mar 4 '07 #19
In article <11**********************@p10g2000cwp.googlegroups .com>,
arnuld <ge*********@gmail.comwrote:
>On Mar 4, 12:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
>arnuld wrote:
>Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.

a "const char*" can easily be modified, see:
Ian said a string literal should not be modified, he did not say...
>#include <iostream>

int main() {
int x = 7;
const char* author = "Stroustrup";

std::cout << x
<< "\t"
<< author
<<"\n";

x = 100;
author = "Bjarne";
... that you cannot reassign to a named const char *.
author is not a string literal in your example, it is a pointer
to one (actually two of them over your apps lifetime) in your case.
std::cout << x
<< "\t"
<< author
<<"\n";
}

---------- OUTPUT
[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra char-po.cpp
[arch@voodo cpp]$ ./a.out
7 Stroustrup
100 Bjarne
[arch@voodo cpp]$
------------------------

no undefined behaviour, i modified the "const char*". now, If i can
modify "const", what is the significance of "const" then ? [1]
You didn''t modify const. The const here, and where UB would come
in, is if you did something like:

*author = 'b';

to make his name lowercase.
>[1] but a "const int/char/float" can not be modified. i have checked
that.
Nor can const other things. But here, the pointer is not const,
the thing is points to is, or at least should be considered so
through author.

And atop that, a string literal, as Ian correctly points out,
should not be considered a modifiable entity.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 4 '07 #20
arnuld wrote:
>On Mar 4, 12:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
>arnuld wrote:
>Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.


a "const char*" can easily be modified, see:
#include <iostream>

int main() {
int x = 7;
const char* author = "Stroustrup";
This means that the *pointee* is const, not the pointer.
std::cout << x
<< "\t"
<< author
<<"\n";

x = 100;
author = "Bjarne";
Here you're modifying the pointer, not the pointee. What the const is
keeping you from writing is for example:

author[0] = 'b'; // Illegal

However, is you had declared author as:

char * const author = "Stroustrup";

the pointer would've been const, but not the pointee :

author = "Bjarne"; // Illegal
author[0] = 'b'; // OK

--
icecrime
Mar 4 '07 #21
On Mar 4, 7:35 pm, com...@panix.com (Greg Comeau) wrote:
author is not a string literal in your example, it is a pointer
to one (actually two of them over your apps lifetime) in your case.
ok, it is a "pointer" to string literal and string literal *is* the
one who is a const.

You didn''t modify const. The const here, and where UB would come
in, is if you did something like:

*author = 'b';

to make his name lowercase.
what is "UB" ?

i did not get your "lowercase" behaviour. may you give an example with
an opposite case too.

Mar 4 '07 #22
In article <45***********************@news.free.fr>,
icecrime <icecrime@epi_remove_tech.netwrote:
>arnuld wrote:
>>On Mar 4, 12:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
arnuld wrote:
Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.


a "const char*" can easily be modified, see:
#include <iostream>

int main() {
int x = 7;
const char* author = "Stroustrup";

This means that the *pointee* is const, not the pointer.
> std::cout << x
<< "\t"
<< author
<<"\n";

x = 100;
author = "Bjarne";

Here you're modifying the pointer, not the pointee. What the const is
keeping you from writing is for example:

author[0] = 'b'; // Illegal

However, is you had declared author as:

char * const author = "Stroustrup";

the pointer would've been const, but not the pointee :

author = "Bjarne"; // Illegal
author[0] = 'b'; // OK
Er, no, the last OK is still UB, which was Ian's point.

See, string literals have this odd duality to them:
the are const char [] (were not always) but have a conversion
(this was done as a compromise else almost every C++ (and C)
program woudl stop working) to char * so that legacy code can
keep working.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 4 '07 #23
In article <11**********************@n33g2000cwc.googlegroups .com>,
arnuld <ge*********@gmail.comwrote:
>On Mar 4, 7:35 pm, com...@panix.com (Greg Comeau) wrote:
author is not a string literal in your example, it is a pointer
to one (actually two of them over your apps lifetime) in your case.

ok, it is a "pointer" to string literal and string literal *is* the
one who is a const.
Um, yes.
>You didn''t modify const. The const here, and where UB would come
in, is if you did something like:

*author = 'b';

to make his name lowercase.

what is "UB" ?
Undefined behavior.
>i did not get your "lowercase" behaviour. may you give an example with
an opposite case too.
I think you had the name as "Bjarne". Using *author = 'b'; is an
attempt to set it to be "bjarne" in memory. That's UB, so it
may or may not work on your system (actually it's probably ill-formed
these days too I'd have to check) and hence should be avoided.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Mar 4 '07 #24
On Mar 4, 8:03 pm, com...@panix.com (Greg Comeau) wrote:
I think you had the name as "Bjarne". Using *author = 'b'; is an
attempt to set it to be "bjarne" in memory. That's UB, so it
may or may not work on your system (actually it's probably ill-formed
these days too I'd have to check) and hence should be avoided.
you want to say that:

char* name1 = "Bjarne"
const char* name2 = "Stroutrup"

name1 will get value 'B' and
name2 will get value 'S', which will be a const

rather than "Bjarne" and "Stroustrup" respecively.

after all of this, if you really think that i am STUPID. then i
request you to please explain it for me or point me to some other
place of information as it is really getting out of my head :-(

thanks

-- arnuld
http://arnuld.blogspot.com

Mar 4 '07 #25
* arnuld:
>On Mar 4, 8:03 pm, com...@panix.com (Greg Comeau) wrote:
>I think you had the name as "Bjarne". Using *author = 'b'; is an
attempt to set it to be "bjarne" in memory. That's UB, so it
may or may not work on your system (actually it's probably ill-formed
these days too I'd have to check) and hence should be avoided.

you want to say that:

char* name1 = "Bjarne"
const char* name2 = "Stroutrup"

name1 will get value 'B' and
name2 will get value 'S', which will be a const

rather than "Bjarne" and "Stroustrup" respecively.
"*name1" refers to the single char that "name1" points to.

That's the first char in a string consisting of the characters 'B', 'j',
'a', 'r', 'n', 'e' and '\0', in succession in memory.

That storage must be regarded as read only because it's a string literal.

The pointer name1 is non-const, what it points to has to be treated as
read only.

Hence "*name1 = 'b';" would set the first character of the string to
'b', resulting in "bjarne" (plus terminating '\0'), except that
modifying a string literal is Undefined Behavior, so may not work.

after all of this, if you really think that i am STUPID. then i
request you to please explain it for me or point me to some other
place of information as it is really getting out of my head :-(
Hey, nobody said anything about stupidity.

We're trying to help.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 4 '07 #26
On Mar 4, 8:58 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
On Mar 4, 8:03 pm, com...@panix.com (Greg Comeau) wrote:
I think you had the name as "Bjarne". Using *author = 'b'; is an
attempt to set it to be "bjarne" in memory. That's UB, so it
may or may not work on your system (actually it's probably ill-formed
these days too I'd have to check) and hence should be avoided.
you want to say that:
char* name1 = "Bjarne"
const char* name2 = "Stroutrup"
name1 will get value 'B' and
name2 will get value 'S', which will be a const
rather than "Bjarne" and "Stroustrup" respecively.

"*name1" refers to the single char that "name1" points to.
oh..K, now i got it.

/name1/ and /*name1/ are same in meaning, pointing to the same "B" of
"Bjarne"

That's the first char in a string consisting of the characters 'B', 'j',
'a', 'r', 'n', 'e' and '\0', in succession in memory.

That storage must be regarded as read only because it's a string literal.

The pointer name1 is non-const, what it points to has to be treated as
read only.

Hence "*name1 = 'b';" would set the first character of the string to
'b', resulting in "bjarne" (plus terminating '\0'), except that
modifying a string literal is Undefined Behavior, so may not work.
after all of this, if you really think that i am STUPID. then i
request you to please explain it for me or point me to some other
place of information as it is really getting out of my head :-(

Hey, nobody said anything about stupidity.
i know, but i felt that i am looking stupid.

We're trying to help.
of course....i am glad you said that

:-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
a long time ago, as a USENET newbie, your words taught me how bad top-
posting is. keep them there, in your sig.

:-)

Mar 4 '07 #27
On 4 Mar, 17:58, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 4, 8:58 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
char* name1 = "Bjarne"
const char* name2 = "Stroutrup"
name1 will get value 'B' and
name2 will get value 'S', which will be a const
rather than "Bjarne" and "Stroustrup" respecively.
"*name1" refers to the single char that "name1" points to.

oh..K, now i got it.

/name1/ and /*name1/ are same in meaning, pointing to the same "B" of
"Bjarne"
Not quite. name1 and *name1 are different. It's possible that Alf's
choice of the word "refers" above has confused you.

name1 _points to_ the 'B' of "Bjarne".

*name1 _is_ the 'B' of "Bjarne".
Hey, nobody said anything about stupidity.

i know, but i felt that i am looking stupid.
Not really. Pointers and C-style strings are tricky to understand. But
it does make sense, and there will be a point where it suddenly clicks
and makes sense.

Gavin Deane

Mar 4 '07 #28
arnuld wrote:
>>On Mar 4, 4:51 pm, "Bo Persson" <b...@gmb.dkwrote:

valid. In C++ string literals have the type 'const char*', but this old C
code has got a special dispensation so that it will continue to work anyway.
Everywhere else it is an error to assign a pointer to const to a pointer to
non-const.

what does last sentence mean:

"a pointer to const to a pointer to non-const"
try compiling

const int n = 42;
const int* p = &n;
int* p1 = p;

--
Ian Collins.
Mar 4 '07 #29
icecrime wrote:
>

Here you're modifying the pointer, not the pointee. What the const is
keeping you from writing is for example:

author[0] = 'b'; // Illegal

However, is you had declared author as:

char * const author = "Stroustrup";

the pointer would've been const, but not the pointee :

author = "Bjarne"; // Illegal
author[0] = 'b'; // OK
Oh look, there goes another exploding toilet!

--
Ian Collins.
Mar 4 '07 #30
>Here you're modifying the pointer, not the pointee. What the const is
>keeping you from writing is for example:

author[0] = 'b'; // Illegal

However, is you had declared author as:

char * const author = "Stroustrup";

the pointer would've been const, but not the pointee :

author = "Bjarne"; // Illegal
author[0] = 'b'; // OK
Oh look, there goes another exploding toilet!
Sorry there :p I was too much into const correctness and ommited that it
is indeed falling into UB !

--
icecrime
Mar 4 '07 #31
arnuld wrote:
>On Mar 4, 4:51 pm, "Bo Persson" <b...@gmb.dkwrote:
arnuld wrote:
>At the time when C++ was designed, C didn't have the keyword const,
so all string literals were of the type char*. That made code like

char* x = "Hello";

valid. In C++ string literals have the type 'const char*', but this
old C code has got a special dispensation so that it will continue
to work anyway. Everywhere else it is an error to assign a pointer
to const to a pointer to non-const.

what does last sentence mean:

"a pointer to const to a pointer to non-const"
this pointer to a char notation:

char* x; or
char *x;
These are entirely equivalent, the placement of the * doesn't matter.

What I meant is that if you have a pointer to const, like

const int* ci;

and a pointer to non-const

int* p;

it is not allowed to assign ci to p, as this would allow us using p to
change what is really const.

p = ci; // NOT allowed
But, with char* you are allowed to do this anyway

char* x = "Hello";

even though this assigns a 'const char*' to a 'char*'. There is nothing to
understand here, and nothing Bjarne is proud of, it just the way it has
always been.

Please stay with std::string, and save this for much later! :-)
>You have now found exactly the reason why many in this group
recommended "Accelerated C++", which introduces std::string on the
first page of chapter
1. The subtle difficulties of pointers and arrays are postponed to
chapter 10, long after teaching about the simpler things like
classes, templates and overloaded functions.

to me, i do not think pointers are difficult than Templates. i do not
have any teacher/mentor who can teach me, except you folks. all of the
colleges here teach a language named VC++ & they do not agree that
"Linux is a good OS" and i am a UNIX man, that is the trouble.
But we have just seen that pointers ARE difficult, and that there are some
dark corners with very special rules. And that sometimes pointers and arrays
behave the same, and sometimes definitely not. This is the C part of C++,
that is not a good idea to try to learn first. It is not very useful,
really.
Bo Persson

Mar 4 '07 #32
On Mar 5, 2:13 am, "Bo Persson" <b...@gmb.dkwrote:
arnuld wrote:
On Mar 4, 4:51 pm, "Bo Persson" <b...@gmb.dkwrote:
arnuld wrote:
At the time when C++ was designed, C didn't have the keyword const,
so all string literals were of the type char*. That made code like
char* x = "Hello";
valid. In C++ string literals have the type 'const char*', but this
old C code has got a special dispensation so that it will continue
to work anyway. Everywhere else it is an error to assign a pointer
to const to a pointer to non-const.
what does last sentence mean:
"a pointer to const to a pointer to non-const"
this pointer to a char notation:
char* x; or
char *x;

These are entirely equivalent, the placement of the * doesn't matter.

What I meant is that if you have a pointer to const, like

const int* ci;

and a pointer to non-const

int* p;

it is not allowed to assign ci to p, as this would allow us using p to
change what is really const.

p = ci; // NOT allowed

But, with char* you are allowed to do this anyway

char* x = "Hello";

even though this assigns a 'const char*' to a 'char*'. There is nothing to
understand here, and nothing Bjarne is proud of, it just the way it has
always been.
ok, finally i got it:
char* author = "Bjarne"

will be implicitly converted to
const char* author = "Bjarne"

/author/ "points" to "B" of "Bjarne"
/*author/ "is" "B" of "Bjarne"

you can change where /author/ points to but that leads to UB. whereas
with:

const int x = 8;
int* y;

doing /y = x/ will not work

right ?

Please stay with std::string, and save this for much later! :-)
sure. i was just trying to understand the "char*" :-)

Mar 5 '07 #33
On 5 Mar, 03:03, "arnuld" <geek.arn...@gmail.comwrote:
ok, finally i got it:

char* author = "Bjarne"

will be implicitly converted to

const char* author = "Bjarne"
You've got that the wrong way round. The string literal "Bjarne" has
the type const char[] (array of const char). When you write

char* author = "Bjarne"

the implicit conversion is from const char[] (array of CONST char) to
char* (pointer to NON-CONST char). You've implicitly converted from
array to pointer and from const to non-const. Array to pointer is fine
- that implicit conversion happens all the time in C++. The awkward
bit that is peculiar to string literals is the implicit conversion
from const to non-const. That doesn't happen anywhere else in c++.
/author/ "points" to "B" of "Bjarne"
/*author/ "is" "B" of "Bjarne"

you can change where /author/ points to but that leads to UB.
No. Changing the *pointer* is fine. Changing *what it points to* leads
to UB.

char* author = "Bjarne";
author = "Stroustrup"; // Example 1. No problem
*author = 's'; // Example 2. UB

There are two separate string literals in that code snippet, "Bjarne"
and "Stroustrup". In example 1, all I am doing is taking author (which
is my pointer) and making it point to a different string literal. I am
not attempting to *modify* either string literal. Example 2 on the
other hand is an attempt to modify the first character of the string
literal "Stroustrup". I am attempting to change the character 'S' into
the character 's'. That leads to UB.
whereas with:

const int x = 8;
int* y;

doing /y = x/ will not work

right ?
Doing y = x will not work because y and x are not the same type -
nothing to do with const. x is an int and y is a pointer. What I think
you meant was

const int x = 8;
int* y;
y = &x;

The statement y = &x does not compile. &x is a pointer to CONST int
and y is a pointer to NON-CONST int. What you are seeing here is the
compiler preventing you from acquiring a pointer that could be used to
modify const data. This is the rule that has to be relaxed for string
literals (for the backwards compatibility reasons already discussed).

Gavin Deane

Mar 5 '07 #34
Geo
On 4 Mar, 10:00, Rolf Magnus <ramag...@t-online.dewrote:
arnuld wrote:
char *name = "hackers";
Is the incorrect way of writing
const char *name = "hackers";
that is really strange for me as i did not know. i collect 2 points
here:
1.) we can use /char*/ where we are damn sure that we need a constant
string.

Yes, however, this is only true if you directly initialize it with a string
literal. That's a stupid special rule to make C++ more compatible with C
code written by sloppy programmers.
sloppy? why do you say that, 'const' is a relatively late addition to
the 'c' language, how could not using a non-existant construct be
considered sloppy ??

Mar 5 '07 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.