Connecting Tech Pros Worldwide Forums | Help | Site Map

Array initialization when defining the array

Spiro Trikaliotis
Guest
 
Posts: n/a
#1: Jun 8 '07
Hello,

in a project, I stumbled upon code like follows (incomplete):

#if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) || (defined(__BEOS__) && defined(WORDS_BIGENDIAN)) || define
d(WATCOM_COMPILE)
void *array[3];

array[0]=&asm6502;
array[1]=&asmz80;
array[2]=NULL;
#else
void *array[3] = { &asm6502, &asmz80, NULL };
#endif

I was very surprised to read this. I always thought the array
initialization (the #else case) was standard since C90. Am I wrong here,
or are the compilers used non-conformant?

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/

Alan Curry
Guest
 
Posts: n/a
#2: Jun 8 '07

re: Array initialization when defining the array


In article <slrnf6j4qp.ao.news-200605@news.trikaliotis.net>,
Spiro Trikaliotis <news-200605@trikaliotis.netwrote:
....
Quote:
>#else
void *array[3] = { &asm6502, &asmz80, NULL };
>#endif
>
>I was very surprised to read this. I always thought the array
>initialization (the #else case) was standard since C90. Am I wrong here,
>or are the compilers used non-conformant?
Depends on whether asm6502 and asmz80 are automatic variables. If they are,
the initializer isn't made up of compile-time constants, which is required
for array initializers in C90.

--
Alan Curry
pacman@world.std.com
Harald van =?UTF-8?B?RMSzaw==?=
Guest
 
Posts: n/a
#3: Jun 8 '07

re: Array initialization when defining the array


Spiro Trikaliotis wrote:
Quote:
Hello,
>
in a project, I stumbled upon code like follows (incomplete):
>
#if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) ||
#(defined(__BEOS__) && defined(WORDS_BIGENDIAN)) || define
d(WATCOM_COMPILE)
void *array[3];
>
array[0]=&asm6502;
array[1]=&asmz80;
array[2]=NULL;
#else
void *array[3] = { &asm6502, &asmz80, NULL };
#endif
>
I was very surprised to read this. I always thought the array
initialization (the #else case) was standard since C90. Am I wrong here,
or are the compilers used non-conformant?
C90 requires array initialisers to be constant. How are asm6502 and asmz80
declared?
Szabolcs Nagy
Guest
 
Posts: n/a
#4: Jun 8 '07

re: Array initialization when defining the array



Spiro Trikaliotis wrote:
Quote:
#if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) || (defined(__BEOS__) && defined(WORDS_BIGENDIAN)) || define
d(WATCOM_COMPILE)
void *array[3];
>
array[0]=&asm6502;
array[1]=&asmz80;
array[2]=NULL;
#else
void *array[3] = { &asm6502, &asmz80, NULL };
#endif
>
it is interesting that someone felt the need to add this ifdef

the second declaration is nicer but with the ifdef that doesn't matter
and the first works on any compiler i guess

the strict condition of array initialization syntax in C90 can be
quite annoying (initializer element should be computable at load time)

CBFalconer
Guest
 
Posts: n/a
#5: Jun 9 '07

re: Array initialization when defining the array


Spiro Trikaliotis wrote:
Quote:
>
in a project, I stumbled upon code like follows (incomplete):
>
#if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) || (defined(__BEOS__) && defined(WORDS_BIGENDIAN)) || define
d(WATCOM_COMPILE)
void *array[3];
>
array[0]=&asm6502;
array[1]=&asmz80;
array[2]=NULL;
#else
void *array[3] = { &asm6502, &asmz80, NULL };
#endif
>
I was very surprised to read this. I always thought the array
initialization (the #else case) was standard since C90. Am I wrong
here, or are the compilers used non-conformant?
For char arrays, initialized with fixed strings. Not here.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

Spiro Trikaliotis
Guest
 
Posts: n/a
#6: Jun 9 '07

re: Array initialization when defining the array


Hello all,

Alan Curry wrote:
Quote:
In article <slrnf6j4qp.ao.news-200605@news.trikaliotis.net>,
Spiro Trikaliotis <news-200605@trikaliotis.netwrote:
[...]
Quote:
Quote:
>>#else
> void *array[3] = { &asm6502, &asmz80, NULL };
>>#endif
[...]
Quote:
Quote:
>>I always thought the array initialization (the #else case) was
>>standard since C90. Am I wrong here, or are the compilers used
>>non-conformant?
>
Depends on whether asm6502 and asmz80 are automatic variables. If they are,
the initializer isn't made up of compile-time constants, which is required
for array initializers in C90.
Indeed, they are automatic variables, defined directly above the code
snippet I presented. And as you all already found out, array is an
automatic variable, too, as the #if part of the code snippet is
intersparsed with code.

Thus, indeed, I was wrong. Oh well, you get so used on compiler
extensions that simply work, that you totally forget that they are
extensions.

Thank you all.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Spiro Trikaliotis
Guest
 
Posts: n/a
#7: Jun 9 '07

re: Array initialization when defining the array


Hello,

Szabolcs Nagy wrote:
Quote:
Spiro Trikaliotis wrote:
Quote:
>#if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) || (defined(__BEOS__) && defined(WORDS_BIGENDIAN)) || define
>d(WATCOM_COMPILE)
[...]
Quote:
Quote:
>#else
> void *array[3] = { &asm6502, &asmz80, NULL };
>#endif
>>
>
it is interesting that someone felt the need to add this ifdef
>
the second declaration is nicer but with the ifdef that doesn't matter
and the first works on any compiler i guess
Indeed. I believe this might be because of some historic reasons. I
assume the #else case was the first implementation. Then, people found
out that some compilers complained (or behaved wrong? The part
"(defined(__BEOS__) && defined(WORDS_BIGENDIAN))" look like the latter
here), and the second implementation was addded. Yes, looking into the
source control, there was more than one year between the implementation
of the #else path, and the adding of the #ifdef.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Closed Thread