Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Wide string initializer syntax

Reply
Thread Tools

Wide string initializer syntax

 
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-11-2004
Looking through the C90 standard, it occurred to me that the possible
syntaxes for initializers, particularly of wchar_t arrays, are really
bizarre. Consider the following:

wchar_t s1[] = { L"abcdef" };
wchar_t* s2[] = { L"abcdef" };
wchar_t s3[][6] = { L"abcdef" };
wchar_t* s4[][6] = { L"abcdef" };

That's four different types initialized with exactly the same
initializer syntax, but it means four different things. In the first
case, a mutable buffer is being initialized, and the standard lets you
wrap the string intializing the buffer in braces for no apparent reason.
In the second case, an array containing one pointer to a literal string
is declared. In the third case, an array containing one initialized
mutable buffer is declared. In the fourth case, a 1 by 6 two-dimensional
array is declared, with s4[0][0] set to a literal string, and s4[0][1]
through s[0][5] set to a null pointer. I could continue with
larger-dimensional arrays right up to the environment limits.

Thoughts?
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      09-11-2004
Derrick Coetzee wrote:
> Looking through the C90 standard, it occurred to me that the possible
> syntaxes for initializers, particularly of wchar_t arrays, are really
> bizarre. Consider the following:


Are you shure that wchar_t is a build-in TYpe for C? I don't know about
C99, but in C90 ther is defently no wchar_t build-in type!

Kind regards,
Nicolas
 
Reply With Quote
 
 
 
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-12-2004
Nicolas Pavlidis wrote:
> Derrick Coetzee wrote:
>
>> Looking through the C90 standard, it occurred to me that the possible
>> syntaxes for initializers, particularly of wchar_t arrays, are really
>> bizarre. Consider the following:

>
>
> Are you shure that wchar_t is a build-in TYpe for C? I don't know about
> C99, but in C90 ther is defently no wchar_t build-in type!


The wchar_t type is not built-in, but is required to be defined in the
standard header stddef.h. Wide string literals are always arrays of
whatever wchar_t is defined to be, even if the type's definition is not
available. The standard mentions wchar_t in several places.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      09-15-2004
In article <news:chu1gr$fni$(E-Mail Removed)>
Derrick Coetzee <(E-Mail Removed)> wrote:
>Looking through the C90 standard, it occurred to me that the possible
>syntaxes for initializers, particularly of wchar_t arrays, are really
>bizarre. Consider the following:
>
> wchar_t s1[] = { L"abcdef" };
> wchar_t* s2[] = { L"abcdef" };
> wchar_t s3[][6] = { L"abcdef" };
> wchar_t* s4[][6] = { L"abcdef" };
>
>That's four different types initialized with exactly the same
>initializer syntax, but it means four different things. ...


Indeed, this is all correct and true, but it is not special to wide
characters. Replace "wchar_t" with "char", and remove the uppercase
L's, and it is still all correct and true.

(Versions of gcc helpfully warn about incomplete/inconsistent
brace-bracketing of the fourth line, given the appropriate options.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-15-2004
Chris Torek wrote:
>> wchar_t s1[] = { L"abcdef" };
>> wchar_t* s2[] = { L"abcdef" };
>> wchar_t s3[][6] = { L"abcdef" };
>> wchar_t* s4[][6] = { L"abcdef" };

>
> Indeed, this is all correct and true, but it is not special to wide
> characters. Replace "wchar_t" with "char", and remove the uppercase
> L's, and it is still all correct and true.


Ah, you're right. It was the first one I was unsure of, but:

"An array of character type may be initialized by a character string
literal, optionally enclosed in braces."
"An array with element type compatible with wchar_t may be initialized
by a wide string literal, optionally enclosed in braces."
- C90, 6.5.7

I can't figure out what these optional braces are for. I suppose yet
another concession to existing implementations.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      09-15-2004
Derrick Coetzee <(E-Mail Removed)> wrote in message news:<ci9jr9$qt5$(E-Mail Removed)>...
>
> "An array of character type may be initialized by a character string
> literal, optionally enclosed in braces."
> "An array with element type compatible with wchar_t may be initialized
> by a wide string literal, optionally enclosed in braces."
> - C90, 6.5.7
>
> I can't figure out what these optional braces are for. I suppose yet
> another concession to existing implementations.


Consistency. In general, initializers for aggregate type are enclosed
in braces.
 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      09-16-2004

In article <(E-Mail Removed) >, http://www.velocityreviews.com/forums/(E-Mail Removed) (J. J. Farrell) writes:
> Derrick Coetzee <(E-Mail Removed)> wrote in message news:<ci9jr9$qt5$(E-Mail Removed)>...
> >
> > I can't figure out what these optional braces are for. I suppose yet
> > another concession to existing implementations.

>
> Consistency. In general, initializers for aggregate type are enclosed
> in braces.


The braces are also optional for initializers for scalar types.

This consistency simplifies things for source-code generators, and
means that {0} is a valid initializer for any object type or any
array of unknown size (in a declaration where initialization is
permitted).

--
Michael Wojcik (E-Mail Removed)
 
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
80 character wide <pre> block appears only 60 character wide onWindows Disc Magnet HTML 2 05-15-2010 06:53 AM
Re: DSLR lenses not good wide open at wide angle? Dauphin de Viennois Digital Photography 2 07-16-2008 12:29 PM
Wide Screen not wide enough? michelebargeman@yahoo.com DVD Video 31 04-27-2006 08:50 PM
Not many "wide-angle" compacts but, heck, many are wide-angle anyway! JeffOYB@hotmail.com Digital Photography 10 01-09-2006 08:30 AM
char 8bit wide or 7bit wide in c++? Web Developer C++ 2 07-31-2003 08:09 AM



Advertisments