The problem with using a pointer instead of an array is that the pointer
points to a string literal and any attempt to modify the literal invokes
undefined behavior. A statement of the form
s6[0] = 'x';
is legal with the array but not with the pointer.
The fact that the expression ptr[index] and index[ptr] are both legal and
equivalent is just an idiosyncrasy of the language. Both will generate the
same code.
:>: -----Original Message-----
:>: From: IBM Mainframe Discussion List [mailto:[email protected]] On
:>: Behalf Of McKown, John
:>: Sent: Wednesday, September 05, 2012 5:35 AM
:>: To: [email protected]
:>: Subject: Re: The IBM zEnterprise EC12 announcment
:>:
:>: What about
:>:
:>: char *s6="wombat";
:>:
:>: ?
:>:
:>: This makes s6 a pointer to the characters
{'w','o','m','b','a','t','\0'}.
:>: There is not really any difference between char s6[] and char *s6. You
:>: can still do s6[0] which is the same as *s6. And s6[1] which is *(s6+1).
:>: s6[n] is just an alternate way to say *(s6+n). Which is identical to
:>: *(n+s6). Which is identical to n[s6]. As weird as that may be. I am not
:>: sure what C++ would do with that.
snip
:>: > -----Original Message-----
:>: > From: IBM Mainframe Discussion List [mailto:[email protected]]
:>: > On Behalf Of Paul Gilmartin
:>: > Sent: Tuesday, September 04, 2012 6:51 PM
:>: > To: [email protected]
:>: > Subject: Re: The IBM zEnterprise EC12 announcment
:>: >
:>: > On Tue, 4 Sep 2012 15:01:37 -0400, Thomas David Rivers wrote:
:>: > >
:>: > > cxx: t.c line 3:Error #144: a value of type "const char [7]"
:>: cannot
:>: > >be used to initialize an entity of type "char [6]"
:>: > > char s6[ 6 ] = "wombat";
:>: > >
:>: > Is there any convenient way to perform this initialization? (I don't
:>: > consider either overallocating the array or enumerating individual
:>: > chars in a multiple initializer convenient.)
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN