Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A question on string literals

Reply
Thread Tools

A question on string literals

 
 
pete
Guest
Posts: n/a
 
      08-13-2005
Chris Barts wrote:
>
> In article <(E-Mail Removed) .com>,
> Sriram Rajagopalan wrote:


> > That is the following is invalid:
> > char arr1[] = { "Hello" };
> > arr1 = 0; // Not allowed.

>
> No, that is still allowed: The array 'decomposes' into a pointer and
> you can modify a non-const-qualified pointer at will.
> Thus, both of these are legal:


Sriram Rajagopalan is right.
The result of the array to pointer conversion is not an lvalue.

>Thus, both of these are legal:
> arr2[1] = 'a'; /* The array is now 'Hallo' */
> arr2 = NULL; /* The array is now unreachable. */


Try to compile it.

--
pete
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-13-2005
Chris Barts wrote:

> In article <(E-Mail Removed) .com>, Sriram
> Rajagopalan wrote:
>>
>> char arr1[] = { "Hello" };
>> arr1 = 0; // Not allowed.


Correct.

>
> No, that is still allowed: The array 'decomposes' into a pointer


Incorrect. Such decay happens only in value contexts, and this is not such a
context.

> and
> you can modify a non-const-qualified pointer at will.


Arrays are not pointers.

> What's giving me fits is what that is a pointer /to/.


It isn't a pointer. It's an array.

> I don't think the
> line of code is valid, in other words. It would be just fine if it was
> defined like this:
>
> char *arr1[] = { "Hello" };


Nothing wrong with the definition, of course, but:

arr1 = NULL; /* wrong */

would still be an error.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-13-2005
Richard Heathfield wrote:
>
> Chris Barts wrote:
>
> > In article <(E-Mail Removed) .com>, Sriram
> > Rajagopalan wrote:
> >>
> >> char arr1[] = { "Hello" };
> >> arr1 = 0; // Not allowed.

>
> Correct.
>
> >
> > No, that is still allowed: The array 'decomposes' into a pointer

>
> Incorrect. Such decay happens only in value contexts,
> and this is not such a context.


I think it does happen. The standard describes the conversion
as something that happens except in three cases.

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators
[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.


> arr1 = NULL; /* wrong */
>
> would still be an error.


What kind of an error?

The name of an array, if not converted,
fits the K&R and the standards' descriptions of lvalue.

Regarding
arr1 = NULL;
my compiler says: error C2106: '=' : left operand must be l-value

--
pete
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-13-2005
pete wrote:

> Richard Heathfield wrote:
>>
>> Chris Barts wrote:
>>
>> > In article <(E-Mail Removed) .com>,
>> > Sriram Rajagopalan wrote:
>> >>
>> >> char arr1[] = { "Hello" };
>> >> arr1 = 0; // Not allowed.

>>
>> Correct.
>>
>> >
>> > No, that is still allowed: The array 'decomposes' into a pointer

>>
>> Incorrect. Such decay happens only in value contexts,
>> and this is not such a context.

>
> I think it does happen. The standard describes the conversion
> as something that happens except in three cases.


It's a constraint violation: "an assignment operator shall have a modifiable
lvalue as its left operand". Therefore, a diagnostic message is required,
and the program is considered incorrect. The Standard does not define how
incorrect programs are translated, or how they behave.

>> arr1 = NULL; /* wrong */
>>
>> would still be an error.

>
> What kind of an error?


A constraint violation.

> The name of an array, if not converted,
> fits the K&R and the standards' descriptions of lvalue.
>
> Regarding
> arr1 = NULL;
> my compiler says: error C2106: '=' : left operand must be l-value


This is in accordance with the Standard, which requires a diagnostic for
that code.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-13-2005
Chris Barts wrote:
[ snip ]
>>
>>That is the following is invalid:
>>char arr1[] = { "Hello" };
>>arr1 = 0; // Not allowed.

>
>
> No, that is still allowed: The array 'decomposes' into a pointer and
> you can modify a non-const-qualified pointer at will.
>

No, that is not allowed. arr1 is an array and you attempt to assign to
it. You cannot assign anything to an array.

> What's giving me fits is what that is a pointer /to/. I don't think the
> line of code is valid, in other words. It would be just fine if it was
> defined like this:
>
> char *arr1[] = { "Hello" };
>
> As it stands, however, I don't think the definition is valid.
>

It is perfectly valid. It initializes arr1[0] (a pointer) with the
address of the literal "Hello".
>
>>3. char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
>>
>>Same comments as above for "arr2" hold true here.

>
>
> Well, no. arr2 is an array of char of size 5, but it cannot be used
> as a string by most standard string-handing functions as it lacks
> nul-termination. An array can decompose into a pointer to the first
> element under certain conditions (such as when it is passed into or
> returned from a function) and that array would decompose into a
> pointer to char. Since nothing is const-qualified, you can modify that
> array's contents and the value of the pointer it can decompose into.
>

I believe you have a concept problem here. The name of an array will
'decay' to the address of its first element in 'rvalue' cases. Used in
an lvalue context, the array name does not decay. It is still an array
and you cannot assign anything to an array.

> Thus, both of these are legal:
>
> arr2[1] = 'a'; /* The array is now 'Hallo' */
> arr2 = NULL; /* The array is now unreachable. */
>

Still not. You cannot assign to an array.

Do you ever try to compile any of this stuff? You might learn something
and avoid some embarrassment.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-13-2005
pete <(E-Mail Removed)> writes:
> Richard Heathfield wrote:

[...]
>> arr1 = NULL; /* wrong */
>>
>> would still be an error.

>
> What kind of an error?
>
> The name of an array, if not converted,
> fits the K&R and the standards' descriptions of lvalue.
>
> Regarding
> arr1 = NULL;
> my compiler says: error C2106: '=' : left operand must be l-value


The error message is slightly misleading. arr1 is an lvalue; the
problem is that it's not a modifiable lvalue.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-14-2005
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> writes:
> > Richard Heathfield wrote:

> [...]
> >> arr1 = NULL; /* wrong */
> >>
> >> would still be an error.

> >
> > What kind of an error?
> >
> > The name of an array, if not converted,
> > fits the K&R and the standards' descriptions of lvalue.
> >
> > Regarding
> > arr1 = NULL;
> > my compiler says: error C2106: '=' : left operand must be l-value

>
> The error message is slightly misleading. arr1 is an lvalue; the
> problem is that it's not a modifiable lvalue.


Why isn't arr1 converted to a nonlvalue pointer in
arr1 = NULL;
as per
N869, 6.3.2.1 Lvalues and function designators, [#3]
?

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-14-2005
Richard Heathfield wrote:
>
> pete wrote:
>
> > Richard Heathfield wrote:
> >>
> >> Chris Barts wrote:
> >>
> >> > In article <(E-Mail Removed) .com>,
> >> > Sriram Rajagopalan wrote:
> >> >>
> >> >> char arr1[] = { "Hello" };
> >> >> arr1 = 0; // Not allowed.
> >>
> >> Correct.
> >>
> >> >
> >> > No, that is still allowed: The array 'decomposes' into a pointer
> >>
> >> Incorrect. Such decay happens only in value contexts,


Where are you getting that from?

--
pete
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-14-2005
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>>
>> pete <(E-Mail Removed)> writes:
>> > Richard Heathfield wrote:

>> [...]
>> >> arr1 = NULL; /* wrong */
>> >>
>> >> would still be an error.
>> >
>> > What kind of an error?
>> >
>> > The name of an array, if not converted,
>> > fits the K&R and the standards' descriptions of lvalue.
>> >
>> > Regarding
>> > arr1 = NULL;
>> > my compiler says: error C2106: '=' : left operand must be l-value

>>
>> The error message is slightly misleading. arr1 is an lvalue; the
>> problem is that it's not a modifiable lvalue.

>
> Why isn't arr1 converted to a nonlvalue pointer in
> arr1 = NULL;
> as per
> N869, 6.3.2.1 Lvalues and function designators, [#3]
> ?


Why do you think it isn't?

Assuming arr1 is an array object:
int arr1[10];
arr1 = NULL;
we know the assignment is illegal. If arr1 is implicitly converted to
a non-lvalue pointer value, that would explain why it's illegal.

One compiler I tried complains "incompatible types in assignment",
which doesn't really fit with this theory, but as long as the compiler
produces a diagnostic that doesn't really matter.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-15-2005
pete wrote:

> Richard Heathfield wrote:
>>
>> pete wrote:
>>
>> >> > No, that is still allowed: The array 'decomposes' into a pointer
>> >>
>> >> Incorrect. Such decay happens only in value contexts,

>
> Where are you getting that from?


Well, I must admit I was getting it from Chris Torek's constant incantations
of "The Rule", and perhaps I was over-interpreting it. The fact remains
that the conversion you expect is not guaranteed by the Standard for the
simple reason that the code itself violates a constraint.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
 
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
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
character literals and string Pete Elmgreen Java 3 11-24-2004 04:42 PM
string literals question copx C Programming 9 09-06-2004 09:44 PM
String literals in Java Harri Pesonen Java 59 06-02-2004 08:00 PM



Advertisments