Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Questions about "char *" and "const char*"

Reply
Thread Tools

Questions about "char *" and "const char*"

 
 
liujiaping
Guest
Posts: n/a
 
      04-05-2007
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";
const char* str6 = "abc";
//str6[0] = 'd'; // of coz error
char* str7 = "abc";
//str7[0] = 'd'; // error too
cout << boolalpha << (str1 == str2) << endl; // false
cout << boolalpha << (str3 == str4) << endl; // false
cout << boolalpha << (str5 == str6) << endl; // true
cout << boolalpha << (str6 == str7) << endl; // true
return 0;
}

I know there are differences between "char*" and "char[]", but I dont
know why. And since *str7 cannot be changed, does this mean that
"char*" is equvalent to "const char*"?

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      04-05-2007
On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
> 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";
> const char* str6 = "abc";
> //str6[0] = 'd'; // of coz error
> char* str7 = "abc";
> //str7[0] = 'd'; // error too
> cout << boolalpha << (str1 == str2) << endl; // false
> cout << boolalpha << (str3 == str4) << endl; // false
> cout << boolalpha << (str5 == str6) << endl; // true
> cout << boolalpha << (str6 == str7) << endl; // true
> return 0;
>
> }
>
> I know there are differences between "char*" and "char[]", but I dont
> know why. And since *str7 cannot be changed, does this mean that
> "char*" is equvalent to "const char*"?


Nope, const char* is const char* and char* is char*. What happens here
is that str7 is a pointer to some location in memory which contains
the string "abc". Now, what is this piece of memory that it's pointing
to? It's a string constant, so even though str7 is not declared to
point to a string constant it does so anyway, just like you can have a
const char* point to a non-constant string.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
yoviesmanda@gmail.com
Guest
Posts: n/a
 
      04-05-2007
On Apr 5, 1:09 pm, "Erik Wikström" <eri...@student.chalmers.se> wrote:
> On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
>
>
>
> > 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";
> > const char* str6 = "abc";
> > //str6[0] = 'd'; // of coz error
> > char* str7 = "abc";
> > //str7[0] = 'd'; // error too
> > cout << boolalpha << (str1 == str2) << endl; // false
> > cout << boolalpha << (str3 == str4) << endl; // false
> > cout << boolalpha << (str5 == str6) << endl; // true
> > cout << boolalpha << (str6 == str7) << endl; // true
> > return 0;

>
> > }


I ever have found this problem. In the source above,
if you want to get or set the value of str7, you can add
"*" before str7. So, you can write " *str7[0] = 'd' ".

why we must add "*", same with Erik say that str7 is pointer.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-05-2007
wrote:
> On Apr 5, 1:09 pm, "Erik Wikström" <eri...@student.chalmers.se> wrote:
>
>>On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
>>
>>>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";
>>> const char* str6 = "abc";
>>> //str6[0] = 'd'; // of coz error
>>> char* str7 = "abc";
>>> //str7[0] = 'd'; // error too
>>> cout << boolalpha << (str1 == str2) << endl; // false
>>> cout << boolalpha << (str3 == str4) << endl; // false
>>> cout << boolalpha << (str5 == str6) << endl; // true
>>> cout << boolalpha << (str6 == str7) << endl; // true
>>> return 0;
>>>}

>
> I ever have found this problem. In the source above,


Well you should, attempting to write to a string literal is undefined
behaviour.

> if you want to get or set the value of str7, you can add
> "*" before str7. So, you can write " *str7[0] = 'd' ".
>

Nonsense, *str7[0] is an attempt to dereference a char!

--
Ian Collins.
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      04-05-2007
On 5 Apr, 09:38, yoviesma...@gmail.com wrote:
> On Apr 5, 1:09 pm, "Erik Wikström" <eri...@student.chalmers.se> wrote:
>
>
>
> > On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:

>
> > > 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";
> > > const char* str6 = "abc";
> > > //str6[0] = 'd'; // of coz error
> > > char* str7 = "abc";
> > > //str7[0] = 'd'; // error too
> > > cout << boolalpha << (str1 == str2) << endl; // false
> > > cout << boolalpha << (str3 == str4) << endl; // false
> > > cout << boolalpha << (str5 == str6) << endl; // true
> > > cout << boolalpha << (str6 == str7) << endl; // true
> > > return 0;

>
> > > }

>
> I ever have found this problem. In the source above,
> if you want to get or set the value of str7, you can add
> "*" before str7. So, you can write " *str7[0] = 'd' ".
>
> why we must add "*", same with Erik say that str7 is pointer.


Nope, *str7[0] would require str7 to be of type char*[] (array of
pointers to char), since you first index into an array and then try to
dereference what you found there.

The reason you can't do str7[0] = 'd' is that the memory location you
are trying to modify is read only.

--
Erik Wikström

 
Reply With Quote
 
liujiaping
Guest
Posts: n/a
 
      04-05-2007
On Apr 5, 2:09 pm, "Erik Wikström" <eri...@student.chalmers.se> wrote:
> On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
>
>
>
> > 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";
> > const char* str6 = "abc";
> > //str6[0] = 'd'; // of coz error
> > char* str7 = "abc";
> > //str7[0] = 'd'; // error too
> > cout << boolalpha << (str1 == str2) << endl; // false
> > cout << boolalpha << (str3 == str4) << endl; // false
> > cout << boolalpha << (str5 == str6) << endl; // true
> > cout << boolalpha << (str6 == str7) << endl; // true
> > return 0;

>
> > }

>
> > I know there are differences between "char*" and "char[]", but I dont
> > know why. And since *str7 cannot be changed, does this mean that
> > "char*" is equvalent to "const char*"?

>
> Nope, const char* is const char* and char* is char*. What happens here
> is that str7 is a pointer to some location in memory which contains
> the string "abc". Now, what is this piece of memory that it's pointing
> to? It's a string constant, so even though str7 is not declared to
> point to a string constant it does so anyway, just like you can have a
> const char* point to a non-constant string.
>
> --
> Erik Wikström


Thanks for your help. So str7 is not constant, but it points to a
constant
string "abc". If both a const char* and a char* point to a non-
constant
string, if the char* changes the string, then the const char* points
to the
string changed by the char* too, am I right? Another question, str1
points to
"abc" too, why can str1 change the string by the statement "str1[0] =
'd';"
but str7 can't? I heard that the constant string "abc" which str7
points to is
in the bss area, and the string str1 point to is in the static area.
What's
the difference between them?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-05-2007
liujiaping wrote:
> On Apr 5, 2:09 pm, "Erik Wikström" <eri...@student.chalmers.se> wrote:
>
>>On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
>>
>>
>>
>>
>>>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";
>>> const char* str6 = "abc";
>>> //str6[0] = 'd'; // of coz error
>>> char* str7 = "abc";
>>> //str7[0] = 'd'; // error too
>>> cout << boolalpha << (str1 == str2) << endl; // false
>>> cout << boolalpha << (str3 == str4) << endl; // false
>>> cout << boolalpha << (str5 == str6) << endl; // true
>>> cout << boolalpha << (str6 == str7) << endl; // true
>>> return 0;
>>>}
>>>I know there are differences between "char*" and "char[]", but I dont
>>>know why. And since *str7 cannot be changed, does this mean that
>>>"char*" is equvalent to "const char*"?

>>
>>Nope, const char* is const char* and char* is char*. What happens here
>>is that str7 is a pointer to some location in memory which contains
>>the string "abc". Now, what is this piece of memory that it's pointing
>>to? It's a string constant, so even though str7 is not declared to
>>point to a string constant it does so anyway, just like you can have a
>>const char* point to a non-constant string.

>
> Thanks for your help. So str7 is not constant, but it points to a
> constant
> string "abc".


It points to a string literal "abc".

> If both a const char* and a char* point to a non-constant
> string, if the char* changes the string, then the const char* points
> to the
> string changed by the char* too, am I right?


Yes, they both point to the same mutable object.

> Another question, str1 points to
> "abc" too, why can str1 change the string by the statement "str1[0] =
> 'd';"


str1 is a (mutable) automatic array. It is not a constant.

> but str7 can't? I heard that the constant string "abc" which str7
> points to is
> in the bss area, and the string str1 point to is in the static area.
> What's
> the difference between them?
>

There isn't a bss area in standard C++, it's an implementation detail.
str1 is an automatic variable, str7 is a pointer to a string literal.

--
Ian Collins.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-05-2007
On Apr 5, 7:35 am, "liujiaping" <ljiap...@gmail.com> wrote:
> 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";
> const char* str6 = "abc";
> //str6[0] = 'd'; // of coz error
> char* str7 = "abc";
> //str7[0] = 'd'; // error too
> cout << boolalpha << (str1 == str2) << endl; // false
> cout << boolalpha << (str3 == str4) << endl; // false
> cout << boolalpha << (str5 == str6) << endl; // true
> cout << boolalpha << (str6 == str7) << endl; // true
> return 0;
> }


> I know there are differences between "char*" and "char[]", but I dont
> know why.


What do you mean, you don't know why? Why on earth should there
not be differences. They're radically different, unrelated
types. char[] is an array, char* a pointer.

> And since *str7 cannot be changed, does this mean that
> "char*" is equvalent to "const char*"?


No. It means that you've stumbled on a loop-hole in the C++
type system. Historically, there wasn't const, and people wrote
`char* p = "abc"', knowing that *p couldn't be modified. When
const was introduced, it was originally felt (in the C
community) that making "abc" const would break too much code, so
they created the idea of a non-const object that you're not
allowed to modify. C++ (where const is a lot more important
than in C) handled the problem slightly differently: "abc" is a
char const[4], and converts implicitly to char const*, but there
is a special, deprecated conversion to char* if (and only if)
the char const* is the result of the array to pointer conversion
on a string literal.

IMHO, it's best to ignore this, and treat `char* s = "abc"' as
an error. Good compilers will warn. (Curiously enough, g++
doesn't, alghough it's usually pretty good with warnings.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      04-05-2007
In article < .com>,
<> wrote:
>On Apr 5, 1:09 pm, "Erik Wikstr=F6m" <eri...@student.chalmers.se> wrote:
>> On 5 Apr, 07:35, "liujiaping" <ljiap...@gmail.com> wrote:
>> > I'm confused about the program below:

>>
>> > int
>> > main(int argc, char* argv[])
>> > {
>> > char str1[] =3D "abc";
>> > char str2[] =3D "abc";
>> > const char str3[] =3D "abc";
>> > const char str4[] =3D "abc";
>> > const char* str5 =3D "abc";
>> > const char* str6 =3D "abc";
>> > //str6[0] =3D 'd'; // of coz error
>> > char* str7 =3D "abc";
>> > //str7[0] =3D 'd'; // error too
>> > cout << boolalpha << (str1 =3D=3D str2) << endl; // false
>> > cout << boolalpha << (str3 =3D=3D str4) << endl; // false
>> > cout << boolalpha << (str5 =3D=3D str6) << endl; // true
>> > cout << boolalpha << (str6 =3D=3D str7) << endl; // true
>> > return 0;
>> > }

>
>I ever have found this problem. In the source above,
>if you want to get or set the value of str7, you can add
>"*" before str7.


But only if it points to a modifiable entity.

Of course, if it is modifiable, it need not only be the first
char that can be modified.

>So, you can write " *str7[0] =3D 'd' ".


No, that's a double dereference (& and []).
So you meant either *str7 = 'd'; or str7[0] = 'd';

However, that runs into another problem: pointing to
a string literal, as the str7 initializer establishes,
means that you should only read from and not write through
that pointer. There reason why is because C or C++ may
put the string in a read only area (and string literals are
literally const in C++ BTW) and also might do something like
merge similar string literals.

Note that this is not the case with say str1[] because post
initialization, str1 is 'a' 'b' 'c' '\0' which is different
than str7 which points to an unnamed array. If one did
str7 = str1; there would not be the string literal problem
mentioned above.

Anyway, so str1 and str2 are distince addresses, hence
the first output will be false, ditto for str3 vs str4.
This compares addresses not content at the addresses.

However, str5 vs str6, and str6 vs str7, may be either
true or false, we don't know which until we run the program
with a specific compiler in a specific mode, and even at that,
it could theoratically be ramdom results, because "abc" here
and "abc" there may or may notbe the same unnamed array.

>why we must add "*", same with Erik say that str7 is pointer.


???
--
Greg Comeau / 4.3.9 with C++0xisms 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?
 
Reply With Quote
 
uglystone
Guest
Posts: n/a
 
      04-05-2007
int
main(int argc, char* argv[])
{
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
//str6[0] = 'd'; // of coz error
char* str7 = "abc";
str7[0] = 'd'; //strange????????
cout << boolalpha << (str1 == str2) << endl; // false
cout << boolalpha << (str3 == str4) << endl; // false
cout << boolalpha << (str5 == str6) << endl; // true
cout << boolalpha << (str6 == str7) << endl; // true
return 0;

}
if str7[0] is uncommented,g++ compiler works well.
but while running the output file,display segement fault!
I feel very strange!
In C++, const char* shouldn't point to a non-constant string.why don't
g++ compiler complain?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
After the Deletion of Google Answers U Got Questions Fills the Gap Answering and Asking the Tough Questions Linux Flash Drives Computer Support 0 05-07-2007 05:38 PM
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments