From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,bc745b8412f53f25 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-03-07 21:42:44 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!zeus.visi.com!news-out.visi.com!green.octanews.net!news-out.octanews.net!news.glorb.com!wn14feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail From: Dave Thompson Newsgroups: comp.lang.ada Subject: Re: Error-names. Message-ID: References: <5tibh1-m92.ln1@martinkl.dialup.fu-berlin.de> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 08 Mar 2004 05:42:44 GMT NNTP-Posting-Host: 12.76.8.12 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1078724564 12.76.8.12 (Mon, 08 Mar 2004 05:42:44 GMT) NNTP-Posting-Date: Mon, 08 Mar 2004 05:42:44 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:6129 Date: 2004-03-08T05:42:44+00:00 List-Id: On Mon, 1 Mar 2004 12:20:37 +0100, Martin Klaiber wrote: > This is right but how can I export the constants then? At the moment > I use in the spec-file: > > -- No error: > No_Error_Code : constant C_Integer := 0; > pragma export (C, No_Error_Code, "no_error_code"); > > -- Fallback: > General_Error_Code : constant C_Integer := -1; > pragma export (C, General_Error_Code, "general_error_code"); > > and so on. The reason is that I want the C-programmer to use the > constants-names instead of the values, because I can't guarantee that > the values will not change in the future. > > BTW: does someone know why these constants are not recognized by the > C-program as constants? They are declared as constants and in the C > header-file I put: > > /* error-codes */ > > extern const int no_error_code; > extern const int general_error_code; > ... > > But if I want to use it in a switch-command like that: > > int year; > ... > for (year = 2003; year <= 2005; ++year) { > switch (set_year (year)) { > case no_error_code : > set_lzz (1); > ... > > the compiler says: > > test_c.c:20: case label does not reduce to an integer constant > 'const' in C is not what C calls a constant, it is a read-only variable (or at least a read-only lvalue = roughly a view). A 'constant', and a 'constant expression', in C is one determinable at compile time, even in the presence of separate compilation; in effect, it must be apparent from the source for one translation unit, that is, one source file plus the file(s) it #include's. This includes literals = 'C' /* characters are actually integers in C */, 123, 4.56, etc.; enum values = enum { foo = 3 } /* only int in C, long if needed in C++ */; expressions depending only on the above; and #define'd macros which after (pre-compile-time) expansion result in any of the above. It does not include any variable, even a const one, in C even an initialized const one; in C++ a const variable (!) of integer or enumeration type -- IF initialized (visibly to the compiler) by a constant expression -- is considered a constant and can be used in case labels, array bounds, etc. But you don't have that either. > Is my exporting wrong? Or is this a C-problem? When I use "case 0" > instead of "case no_error_code" it works ok, but this is what I want > to avoid. > This is a limitation of C, that case labels must be (separate) compile-time constants. Use an if/else chain; or export your constants as *source* #define's or enum's; or code-generate your C source instantiating the actual numbers -- which is effectively equivalent to the #define approach implemented "by hand". - David.Thompson1 at worldnet.att.net