[comp.std.c] Initialisation of Character Arrays

bj@europa.inmos.co.uk (John Honniball) (11/15/90)

Consider the following code:

static          char s1[] = "String One";
static unsigned char s2[] = "String Two";
static signed   char s3[] = "String Three";

Just three initialised character arrays, right?  Now compile it with
GNU C (version 1.36):

gcc -c -ansi -pedantic u.c
u.c:2: warning: ANSI C forbids string initializer except for `char' elements
u.c:3: warning: ANSI C forbids string initializer except for `char' elements

So the compiler is complaining about the initialised 'unsigned' and
'signed' character arrays.  Paragraph 3.5.7 of the ANSI Standard says:

   An array of character type may be initialized by a character string
literal, optionally enclosed in braces.  Successive characters of the
character string literal ... initialize the elements of the array.

The question is, does the code above comply with the Standard?

________________________________________________________________
| John Honniball (BJ), INMOS Limited, 1000 Aztec West, Bristol |
| UK: bj@inmos.co.uk   Elsewhere: bj@inmos.com                 |

pds@lemming.webo.dg.com (Paul D. Smith) (11/16/90)

[] Consider the following code:

[] static          char s1[] = "String One";
[] static unsigned char s2[] = "String Two";
[] static signed   char s3[] = "String Three";

[...]

[] gcc -c -ansi -pedantic u.c
[] u.c:2: warning: ANSI C forbids string initializer except for `char' elements
[] u.c:3: warning: ANSI C forbids string initializer except for `char' elements

[...]  Paragraph 3.5.7 of the ANSI Standard says:

[]    An array of character type may be initialized by a character string
[] literal, optionally enclosed in braces.  Successive characters of the
[] character string literal ... initialize the elements of the array.

[] The question is, does the code above comply with the Standard?

I had the same question when I got that same error.  I also was not
able to find anything truly specific (all I've got is K&R II).  In
Section A8.7, p. 219, you can find the above quote (almost verbatim),
and also the following:

  ... a wide character literal may initialize an array of type
  `wchar_t'.

It's unfortunate the standard says `character' instead of `type char'
if they didn't mean to include explicitly signed or unsigned chars; in
light of the specific reference to `wchar_t' type I assumed that if
they had not meant to include signed and unsigned chars they would
have been more precise ...

But, I changed my code anyway ... a pain, but I hate warnings ...
--

                                                                paul
-----
 ------------------------------------------------------------------
| Paul D. Smith                          | pds@lemming.webo.dg.com |
| Data General Corp.                     |                         |
| Network Services Development           |   "Pretty Damn S..."    |
| Open Network Applications Department   |                         |
 ------------------------------------------------------------------

gwyn@smoke.brl.mil (Doug Gwyn) (11/16/90)

In article <12585@ganymede.inmos.co.uk> bj@inmos.co.uk (John Honniball) writes:
-static          char s1[] = "String One";
-static unsigned char s2[] = "String Two";
-static signed   char s3[] = "String Three";
-gcc -c -ansi -pedantic u.c
-u.c:2: warning: ANSI C forbids string initializer except for `char' elements
-u.c:3: warning: ANSI C forbids string initializer except for `char' elements

Compiler error.  3.5.7, as you pointed out, says that an array of
character type may be initialized by a character string literal.
"Character type" is formally defined in 3.1.2.5.