473,503 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wide string initializer syntax

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.
Nov 14 '05 #1
6 2281
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
Nov 14 '05 #2
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.
Nov 14 '05 #3
In article <news:ch**********@news-int.gatech.edu>
Derrick Coetzee <dc****@moonflare.com> 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 (40°39.22'N, 111°50.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.
Nov 14 '05 #4
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.
Nov 14 '05 #5
Derrick Coetzee <dc****@moonflare.com> wrote in message news:<ci**********@news-int.gatech.edu>...

"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.
Nov 14 '05 #6

In article <5c**************************@posting.google.com >, jj*@bcs.org.uk (J. J. Farrell) writes:
Derrick Coetzee <dc****@moonflare.com> wrote in message news:<ci**********@news-int.gatech.edu>...

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 mi************@microfocus.com
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2977
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
9
26975
by: Niels Dekker - no reply address | last post by:
Are all the following initializations semantically equivalent? wchar_t a = {L'\0'}; wchar_t b = {'\0'}; wchar_t c = {0}; wchar_t d = {}; If so, why don't we all use an empty initializer...
8
4169
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
7
4345
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
5
2278
by: Wu | last post by:
Hi there, I was wondering whether there is a way to use array initializer syntax to initialize a non-static array member in a class in C++. In particular, if I have a class Foo: class Foo {...
4
17735
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
6
1625
by: chandanlinster | last post by:
hello everybody, consider the following statement, char *s = "someString"; Does the above statement cause "someString" to be alloted a constant memory space. What I mean is can't we...
14
4045
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
10
2714
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.