delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2011/08/26/18:45:07

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: Rugxulo <rugxulo AT gmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Array initialization question
Date: Fri, 26 Aug 2011 15:33:36 -0700 (PDT)
Organization: http://groups.google.com
Lines: 58
Message-ID: <d68b38f3-7d55-4d62-b36d-a61698d9f3c2@x2g2000yql.googlegroups.com>
References: <j393ip$q9n$1 AT news DOT albasani DOT net>
NNTP-Posting-Host: 65.13.115.246
Mime-Version: 1.0
X-Trace: posting.google.com 1314398353 27065 127.0.0.1 (26 Aug 2011 22:39:13 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: Fri, 26 Aug 2011 22:39:13 +0000 (UTC)
Complaints-To: groups-abuse AT google DOT com
Injection-Info: x2g2000yql.googlegroups.com; posting-host=65.13.115.246; posting-account=p5rsXQoAAAB8KPnVlgg9E_vlm2dvVhfO
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: HUALESNKRC
X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0,gzip(gfe)
Bytes: 3473
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id p7QMj2Bc024395
Reply-To: djgpp AT delorie DOT com

Hi,
   You're lucky that I was just learning about this yesterday (as I'm
not really a C coder, barely)!

On Aug 27, 5:36am, Mok-Kong Shen <mok-kong DOT s DOT  DOT  DOT  AT t-online DOT de> wrote:
>
> I found that lines like
>
>  const int sz=3;
>  int arr[sz];
>
> seem to work well as C code. However I can't have an initialization list
> e.g.
>
>  int arr[sz]={ 0,1,2 };

Confusing stuff. Apparently it doesn't work. You can have "variable
length arrays" but not initialize them at runtime??

http://gcc.gnu.org/c99status.html

"Variable length arrays" are determined at runtime as opposed to true
compile time constant (#define). "sz" is a read-only variable that is
initialized at startup to value 3. So normal C89 (and apparently even
C99) doesn't like this runtime array initialization. Anyways, GCC
(still) is only "-std=gnu89" by default ("until C99 is fully
implemented??). So if you have GCC 4.5.x, try "-std=c99" for VLAs.
(BTW, I find it interesting that Clang is C99 compatible by default,
but there's obviously no DOS port, heheh.)

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

Actually, at first I thought you were referring to "compound literals"
but apparently not. (Also related: "designated initializers".)

http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Compound-Literals.html
http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

Obligatory other intersting links:

http://en.wikipedia.org/wiki/C99
http://www.openwatcom.org/index.php/C99_Compliance

> On the other hand such initialization lists seem to be ok for codes
> running in Visual C++. Is this due to a difference between standards
> of C and C++?

Probably, yes, though I'm not sure how or why (as I know literally nil
about C++). The latest C++0x / C++11 (finalized but upcoming
publication) standard has better C99 compatibility. But MSVC 2010
still doesn't support C99 at all, they prefer to focus on C++. There
is some minimal C++11 support in various compilers (e.g. GCC 4.3.x on
up), but of course nothing fully yet. (I count 18 "no"s of unsupported
stuff listed on GCC's page, heheh, and they're apparently one of the
best re: compliance so far.)

http://gcc.gnu.org/projects/cxx0x.html
http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019