Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference between between these "char"s

Reply
Thread Tools

difference between between these "char"s

 
 
icecrime
Guest
Posts: n/a
 
      03-04-2007
>> 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 I was too much into const correctness and ommited that it
is indeed falling into UB !

--
icecrime
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      03-04-2007
arnuld wrote:
>> On Mar 4, 4:51 pm, "Bo Persson" <(E-Mail Removed)> wrote:
>> 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



 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      03-05-2007
On 5 Mar, 03:03, "arnuld" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Geo
Guest
Posts: n/a
 
      03-05-2007
On 4 Mar, 10:00, Rolf Magnus <(E-Mail Removed)> wrote:
> 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 ??

 
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
what's the difference between these two methods? (aka, why doesn't one of them work?) JohnJSal Python 13 11-04-2006 08:17 PM
What's the difference between these 2 statements? ATSkyWalker Python 11 04-20-2005 08:49 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Difference between these two class declaration Rach Java 2 06-09-2004 11:50 AM
what is the difference between these two declarations Guybrush Threepwood C++ 7 02-26-2004 11:24 PM



Advertisments