473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Puzzling compiler response to array initialization.

Hello. I am puzzled. A line of the form

char array[] = { a};

or

char array[] = { a, b, c};

is an array initializer.

_Except_

102$ cat try.c
#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };
char at[] = { "Hello", "Goodbye." };

return 0;
} /* main */

103$ cc -W try.c
try.c: In function `main':
try.c:6: excess elements in char array initializer
try.c:6: (near initialization for `at')

Line 5 passes, but it should get the same error as line 6.

--
Michael Press
Nov 15 '05 #1
18 2457
Michael Press wrote:

Hello. I am puzzled. A line of the form

char array[] = { a};

or

char array[] = { a, b, c};

is an array initializer.

_Except_

102$ cat try.c
#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };
char at[] = { "Hello", "Goodbye." };
char *at[] = { "Hello", "Goodbye." };

return 0;
} /* main */

103$ cc -W try.c
try.c: In function `main':
try.c:6: excess elements in char array initializer
try.c:6: (near initialization for `at')

Line 5 passes, but it should get the same error as line 6.


There's nothing wrong with line 5.

char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};

{ "Hello", "Goodbye." } is an initializer for an array of pointers.

--
pete
Nov 15 '05 #2
pete wrote:

Michael Press wrote:

Hello. I am puzzled. A line of the form

char array[] = { a};

or

char array[] = { a, b, c};

is an array initializer.

_Except_

102$ cat try.c
#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };
char at[] = { "Hello", "Goodbye." };


char *at[] = { "Hello", "Goodbye." };

return 0;
} /* main */

103$ cc -W try.c
try.c: In function `main':
try.c:6: excess elements in char array initializer
try.c:6: (near initialization for `at')

Line 5 passes, but it should get the same error as line 6.


There's nothing wrong with line 5.

char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};


Excuse me. I meant

char ao[] = { 'H','e','l','l','o','\0'};
--
pete
Nov 15 '05 #3
Michael Press <ja**@abc.net> wrote in news:jack-B9EA52.19181703102005
@newsclstr02.news.prodigy.com:
Hello. I am puzzled. A line of the form

char array[] = { a};
Well, no. Either

char array[] = { 'a' };

or,

char array[] = "a";
char array[] = { a, b, c};
Similarly:

char array[] = { 'a', 'b', 'c', };
102$ cat try.c
#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };
The RHS is an array of char *.

So,

char *ao[] = { "Hello" };
char at[] = { "Hello", "Goodbye." };


char *at[] = { "Hello", "Goodbye.", };

/* --- */
#include <stdio.h>

int main () {
size_t i;
char ac[] = { 'H', 'e', 'l', 'l', 'o', };
char *at[] = { "Hello", "Goodbye.", "etc" };
for (i = 0; i != sizeof(at)/sizeof(at[0]); ++i) {
puts(at[i]);
}
for (i = 0; i != sizeof(ac); ++i) {
printf("%c\n", ac[i]);
}
return 0;
}
/* --- */

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
Nov 15 '05 #4
pete <pf*****@mindspring.com> wrote:
char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};

^^^^
ITYM '\0', presumably?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5
A. Sinan Unur wrote:

Michael Press <ja**@abc.net> wrote in news:jack-B9EA52.19181703102005

#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };


The RHS is an array of char *.


It can be, but it isn't.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char ao[] = { "Hello" };
char *at[] = { "World" };

puts(ao );
puts(at[0]);
return 0;
}

/* END new.c */

--
pete
Nov 15 '05 #6
Christopher Benson-Manica wrote:

pete <pf*****@mindspring.com> wrote:
char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};

^^^^
ITYM '\0', presumably?


Yes.

--
pete
Nov 15 '05 #7
pete <pf*****@mindspring.com> wrote in news:43*********@mindspring.com:
A. Sinan Unur wrote:

Michael Press <ja**@abc.net> wrote in news:jack-B9EA52.19181703102005

> #include <stdio.h>
>
> int main ()
> {
> char ao[] = { "Hello" };


The RHS is an array of char *.


It can be, but it isn't.


Yes. I had meant to put that comment further below. Sorry about that.

Sinan

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
Nov 15 '05 #8
pete <pf*****@mindspring.com> wrote:
Yes.


I saw your correction after I posted mine. My apologies.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #9
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Michael Press wrote:

Hello. I am puzzled. A line of the form

char array[] = { a};

or

char array[] = { a, b, c};

is an array initializer.

_Except_

102$ cat try.c
#include <stdio.h>

int main ()
{
char ao[] = { "Hello" };
char at[] = { "Hello", "Goodbye." };
char *at[] = { "Hello", "Goodbye." };

return 0;
} /* main */

103$ cc -W try.c
try.c: In function `main':
try.c:6: excess elements in char array initializer
try.c:6: (near initialization for `at')

Line 5 passes, but it should get the same error as line 6.


There's nothing wrong with line 5.

char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};


Where does the standard say this?

"Hello"

evaluates to a pointer to char.
{ "Hello", "Goodbye." } is an initializer for an array of pointers.


Yes, I know this.

I assert that there is something wrong with line 5. An
item in an initializer-list to an array of char should
evaluate to a char, and "Hello" does not evaluate to a
char.

I do not have at hand a Backus-Naur grammar for C; only
K&R 1st edition, and it does not allow the initializer

char ao[] = { "Hello" };

Can anyone point me at an up to date Backus-Naur grammar
for C?

To be more plain

char at[] = { "Hello", "Goodbye." };

is manifestly incorrect, but

char ao[] = { "Hello" };

is incorrrect by the same reasoning.

?

--
Michael Press
Nov 15 '05 #10
In article <Xn****************************@127.0.0.1>,
"A. Sinan Unur" <1u**@llenroc.ude.invalid> wrote:
Michael Press <ja**@abc.net> wrote in news:jack-B9EA52.19181703102005
@newsclstr02.news.prodigy.com:
Hello. I am puzzled. A line of the form

char array[] = { a};


Well, no. Either

char array[] = { 'a' };

or,

char array[] = "a";
char array[] = { a, b, c};


Similarly:

char array[] = { 'a', 'b', 'c', };


I did not specify a; simply meant it to be valid for an
initilializer. For instance

char a = (char) 'a';
char array[] = {a};

I apologize for confusing the matter.

--
Michael Press
Nov 15 '05 #11
Michael Press wrote:

In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Michael Press wrote: char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};


Where does the standard say this?

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.

--
pete
Nov 15 '05 #12
pete wrote:

Michael Press wrote:

In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Michael Press wrote: char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};

That's supposed to end with a '\0' instead of a '\n'

N869
6.4.5 String literals

[#5] In translation phase 7, a byte or code of value zero is
appended to each multibyte character sequence that results
from a string literal or literals. The multibyte
character sequence is then used to initialize an array of
static storage duration and length just sufficient to
contain the sequence.
Where does the standard say this?


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.


--
pete
Nov 15 '05 #13
pete wrote:

pete wrote:

Michael Press wrote:

In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:

> Michael Press wrote:

> char ao[] = { "Hello" };
> is shorthand for:
> char ao[] = { 'H','e','l','l','o','\n'};
That's supposed to end with a '\0' instead of a '\n'

Where does the standard say this?


N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators


This is probably a better quote here:

N869
6.7.8 Initialization

[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.

--
pete
Nov 15 '05 #14
Michael Press wrote:
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
<snip>
char ao[] = { "Hello" };
is shorthand for:
char ao[] = { 'H','e','l','l','o','\n'};

Where does the standard say this?

"Hello"

evaluates to a pointer to char.


Not when used to initialise an array of char it doesn't.
{ "Hello", "Goodbye." } is an initializer for an array of pointers.

Yes, I know this.

I assert that there is something wrong with line 5. An
item in an initializer-list to an array of char should
evaluate to a char, and "Hello" does not evaluate to a
char.


You are wrong.
I do not have at hand a Backus-Naur grammar for C; only
K&R 1st edition, and it does not allow the initializer

char ao[] = { "Hello" };
K&R1 is hopelessly out of date. There were a number of important changes
in the language after it was released so you should get a copy of K&R2
which has been out for over 15 years.
Can anyone point me at an up to date Backus-Naur grammar
for C?
I can't, but there are several draft versions of the standard available
legally for free on the net, I'm using n1124.pdf at the moment.
To be more plain

char at[] = { "Hello", "Goodbye." };

is manifestly incorrect, but

char ao[] = { "Hello" };

is incorrrect by the same reasoning.

?


In N1124 section 6.8.7 Initialization there is the following text:
| 14 An array of character type may be initialized by a character string
| literal, optionally enclosed in braces. Successive characters of
| the character string literal (including the terminating null
| character if there is room or if the array is of unknown size)
| initialize the elements of the array.

I believe this has been the case since at least the original ANSI
standard published in 1989, although the wording may have change.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #15
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
pete wrote:

pete wrote:

Michael Press wrote:
>
> In article <43***********@mindspring.com>,
> pete <pf*****@mindspring.com> wrote:
>
> > Michael Press wrote:

> > char ao[] = { "Hello" };
> > is shorthand for:
> > char ao[] = { 'H','e','l','l','o','\n'};


That's supposed to end with a '\0' instead of a '\n'

> Where does the standard say this?

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators


This is probably a better quote here:

N869
6.7.8 Initialization

[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.


Thank you.

Next question. Why did the standard make a special case of
char a[] = {"Hello"}; ? If the special case did not exist
the code would precipitate a compile time error. Who needs
that construction?

--
Michael Press
Nov 15 '05 #16
Michael Press wrote:

In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
pete wrote:

pete wrote:
>
> Michael Press wrote:
> >
> > In article <43***********@mindspring.com>,
> > pete <pf*****@mindspring.com> wrote:
> >
> > > Michael Press wrote:
>
> > > char ao[] = { "Hello" };
> > > is shorthand for:
> > > char ao[] = { 'H','e','l','l','o','\n'};

That's supposed to end with a '\0' instead of a '\n'

> > Where does the standard say this?
>
> N869
> 6.3.2 Other operands
> 6.3.2.1 Lvalues and function designators


This is probably a better quote here:

N869
6.7.8 Initialization

[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.


Thank you.

Next question. Why did the standard make a special case of
char a[] = {"Hello"}; ? If the special case did not exist
the code would precipitate a compile time error. Who needs
that construction?


Almost every initialization of an array of char
with a string is done that way.

Who wants to write { 'H','e','l','l','o','\0'}
when they can just write "Hello"?
As you can see, I couldn't even spell { 'H','e','l','l','o','\0' }
correctly on my first attempt.

char string[] = "... and when the string is longer, "
"the other way is really tedious.";

--
pete
Nov 15 '05 #17
pete <pf*****@mindspring.com> writes:
Michael Press wrote:
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote: [...]
> N869
> 6.7.8 Initialization
>
> [#14] An array of character type may be initialized by a
> character string literal, optionally enclosed in braces.
> Successive characters of the character string literal
> (including the terminating null character if there is room
> or if the array is of unknown size) initialize the elements
> of the array.


Thank you.

Next question. Why did the standard make a special case of
char a[] = {"Hello"}; ? If the special case did not exist
the code would precipitate a compile time error. Who needs
that construction?


Almost every initialization of an array of char
with a string is done that way.

Who wants to write { 'H','e','l','l','o','\0'}
when they can just write "Hello"?
As you can see, I couldn't even spell { 'H','e','l','l','o','\0' }
correctly on my first attempt.

char string[] = "... and when the string is longer, "
"the other way is really tedious.";


Well, of course. I think the question was why the standard allows
char s[] = { "hello" };
as well as
char s[] = "hello";

I suspect the point is that all initializations can be enclosed in
braces, even
int n = { 42 };

I don't really see the point myself; perhaps someone who does can
explain the rationale.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #18

In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.org> writes:

I suspect the point is that all initializations can be enclosed in
braces, even
int n = { 42 };

I don't really see the point myself; perhaps someone who does can
explain the rationale.


For one thing, it means {0} is a valid initializer for an object of
any (complete, object) type.

Possible considerations that come to mind: it simplifies generating
source code programmatically; it allows for a certain consistency
of style; it's similar to the rule permitting extraneous parentheses
around expressions; and it doesn't hurt anything.

--
Michael Wojcik mi************@microfocus.com

Thanks for your prompt reply and thanks for your invitatin to your
paradise. Based on Buddihism transmigration, I realize you, European,
might be a philanthropist in previous life! -- supplied by Stacy Vickers
Nov 15 '05 #19

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

Similar topics

22
by: Canonical Latin | last post by:
#include<iostream> int main() { char buff; std::cin.getline(buff,3); std::cin.getline(buff,3); std::cout << buff << endl; } Run at command prompt and input 1234567 what do you get as output?
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: Hexman | last post by:
Code below ---- I've asked a similar question on this forum earlier. This is a slightly different situation. Previous Question ---- I'm trying to save some specific web pages to disk as...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
6
by: tropos | last post by:
For my sins, I'm maintaining some old C code which is migrated to C++. Dozens of lines of it looks like this: char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , " " from (" " select...
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
16
by: Vols | last post by:
char s = "1234567"; when we use C compiler (gcc), it works fine. An error is reported if using C++ compiler (g++). any story for this one? Vol
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.