Mail Archives: djgpp/2011/08/26/18:45:07

X-Authentication-Warning: 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)
Lines: 58
Message-ID: <>
References: <j393ip$q9n$1 AT news DOT albasani DOT net>
Mime-Version: 1.0
X-Trace: 1314398353 27065 (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:; posting-host=; 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 id p7QMj2Bc024395
Reply-To: djgpp AT delorie DOT com

   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??

"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.)

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

Obligatory other intersting links:

> 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.)

- Raw text -

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