By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

difference between between these "char"s

P: n/a
what is the difference between these 2:

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

Mar 4 '07 #1
Share this Question
Share on Google+
34 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.