Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Puzzling compiler response to array initialization.

Reply
Thread Tools

Puzzling compiler response to array initialization.

 
 
Michael Press
Guest
Posts: n/a
 
      10-04-2005
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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-04-2005
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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      10-04-2005
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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-04-2005
Michael Press <> 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 <>
(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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-04-2005
pete <> 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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-04-2005
A. Sinan Unur wrote:
>
> Michael Press <> 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-04-2005
Christopher Benson-Manica wrote:
>
> pete <> wrote:
>
> > char ao[] = { "Hello" };
> > is shorthand for:
> > char ao[] = { 'H','e','l','l','o','\n'};

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


Yes.

--
pete
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-04-2005
pete <> wrote in news::

> A. Sinan Unur wrote:
>>
>> Michael Press <> 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 <>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-04-2005
pete <> 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.
 
Reply With Quote
 
Michael Press
Guest
Posts: n/a
 
      10-04-2005
In article <>,
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'};


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
 
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
Puzzling bug with yielded array A. S. Bradbury Ruby 10 09-10-2006 03:25 PM
Puzzling VPN problem with Windows 2003 John Rennie Cisco 0 03-14-2006 07:19 PM
Puzzling Browser Refresh and session attribute behavior GIMME Java 1 04-14-2004 11:27 PM
puzzling routing problem Geert Cisco 0 04-07-2004 10:28 PM
Puzzling issue in casting a class GaryM Java 0 12-21-2003 01:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57