Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const is not const

Reply
Thread Tools

const is not const

 
 
Szabolcs Nagy
Guest
Posts: n/a
 
      11-07-2007
is there a reason why const is not compile-time constant?

the usual example, where it'd be nice is array declaration:
const int N = 4;
float arr[N]

or one may want to use an enum for indices:
enum index {a,b,c,d,N};
float arr[N]

of course both are wrong and the solution is an ugly define
#define N 5

now i found another example where it would be nice to be able to
define compile-time constants:
const char * const a[] = {"a", "bb", "ccc"};
const char *b[] = {a[1], a[0], a[2], a[2]};

this is an error because a[i] is not constant, which is a problem
since i don't know any elegant way to define b[] (let's say a and b
are globals in a one module c code)

(same problem with struct initialization)

any ideas?

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      11-07-2007
Szabolcs Nagy wrote:
> is there a reason why const is not compile-time constant?
>
> the usual example, where it'd be nice is array declaration:
> const int N = 4;
> float arr[N]
>
> or one may want to use an enum for indices:
> enum index {a,b,c,d,N};
> float arr[N]
>
> of course both are wrong and the solution is an ugly define
> #define N 5
>


The lcc-win32 C compiler accepts this as an extension.

I am not sure I got all the cases right though. The above example
compiles without problems. More problematic are the cases where the
constant "variable" would be used in a switch for instance.

> now i found another example where it would be nice to be able to
> define compile-time constants:
> const char * const a[] = {"a", "bb", "ccc"};
> const char *b[] = {a[1], a[0], a[2], a[2]};
>
> this is an error because a[i] is not constant, which is a problem
> since i don't know any elegant way to define b[] (let's say a and b
> are globals in a one module c code)
>
> (same problem with struct initialization)
>
> any ideas?
>




const char * const a[] = {"a", "bb", "ccc"};
const char **b[] = {&a[1], &a[0], &a[2], &a[2]};

That works but is ugly...

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-07-2007
Szabolcs Nagy wrote:
> is there a reason why const is not compile-time constant?
>
> the usual example, where it'd be nice is array declaration:
> const int N = 4;
> float arr[N]


It is OK in C99, but not OK in C89/90.

> or one may want to use an enum for indices:
> enum index {a,b,c,d,N};
> float arr[N]
>
> of course both are wrong and the solution is an ugly define
> #define N 5


The version with enum is OK even in the "original" ANSI C (C89/90). I don't know
why you call it "wrong".

> now i found another example where it would be nice to be able to
> define compile-time constants:
> const char * const a[] = {"a", "bb", "ccc"};
> const char *b[] = {a[1], a[0], a[2], a[2]};
>
> this is an error because a[i] is not constant, which is a problem
> since i don't know any elegant way to define b[] (let's say a and b
> are globals in a one module c code)
>
> (same problem with struct initialization)


Also OK in C99 for non-static objects.

> any ideas?


Well, it was probably done this way just to make things a bit simpler in the
original revisions of the language. Simpler to the compiler implementers that is.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-07-2007
Szabolcs Nagy wrote On 11/07/07 09:01,:
> is there a reason why const is not compile-time constant?


If someone answered "No," would you believe it?

> the usual example, where it'd be nice is array declaration:
> const int N = 4;
> float arr[N]


Think about this one for a few moments:

extern const int N; /* defined elsewhere */
float arr[N]; /* ... but with what value? */

> or one may want to use an enum for indices:
> enum index {a,b,c,d,N};
> float arr[N]


This is fine (after adding a semicolon).

> of course both are wrong


No, only the first fails.

> and the solution is an ugly define
> #define N 5
>
> now i found another example where it would be nice to be able to
> define compile-time constants:
> const char * const a[] = {"a", "bb", "ccc"};
> const char *b[] = {a[1], a[0], a[2], a[2]};
>
> this is an error because a[i] is not constant, which is a problem
> since i don't know any elegant way to define b[] (let's say a and b
> are globals in a one module c code)


Do you prefer "elegant" to "effective?" If so,
do not use

const char astring[] = "a";
const char bstring[] = "bb";
const char cstring[] = "ccc";
const char * const a[] = { astring, bstring, cstring };
const char * b[] = {
bstring, astring, cstring, cstring };

> (same problem with struct initialization)


Same solution, unless it fails to meet your
standards of elegance.

> any ideas?


Learn how your tools work. Then use them if they work
well enough for your purposes, or abandon them and choose
other tools if not. Don't waste time whining that your
Phillips screwdriver doesn't turn Torx bolts.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      11-07-2007
Andrey Tarasevich wrote:
> Szabolcs Nagy wrote:
>> is there a reason why const is not compile-time constant?
>>
>> the usual example, where it'd be nice is array declaration:
>> const int N = 4;
>> float arr[N]

>
> It is OK in C99, but not OK in C89/90.
>


Only within a function scope. Not at the global level.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-07-2007
Szabolcs Nagy <(E-Mail Removed)> writes:
> is there a reason why const is not compile-time constant?


Historical reasons. The "const" keyword didn't originally exist in C.
It was added in a way that tried not to break existing code.

Really "const" doesn't mean "constant"; it means "read-only".

[...]

> or one may want to use an enum for indices:
> enum index {a,b,c,d,N};
> float arr[N]
>
> of course both are wrong and the solution is an ugly define
> #define N 5


An enum is actually a common way to declare a true constant:

enum { N = 5 };

This is arguably an abuse of "enum", since that's not really what it
was intended for, but it's sufficiently useful that I don't mind that.
Also, it's limited to declaring constants of type int.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Szabolcs Nagy
Guest
Posts: n/a
 
      11-07-2007

Eric Sosman wrote:
> Szabolcs Nagy wrote On 11/07/07 09:01,:
> > is there a reason why const is not compile-time constant?

>
> If someone answered "No," would you believe it?

sure

> Think about this one for a few moments:
>
> extern const int N; /* defined elsewhere */
> float arr[N]; /* ... but with what value? */

ok so it's extern's fault (damn you extern)

> > or one may want to use an enum for indices:
> > enum index {a,b,c,d,N};
> > float arr[N]

>
> This is fine (after adding a semicolon).

sorry, for some reason i thought enum is not ok there and i was lazy
to try it out..

> > this is an error because a[i] is not constant, which is a problem
> > since i don't know any elegant way to define b[] (let's say a and b
> > are globals in a one module c code)

>
> Do you prefer "elegant" to "effective?" If so,
> do not use
>
> const char astring[] = "a";
> const char bstring[] = "bb";
> const char cstring[] = "ccc";
> const char * const a[] = { astring, bstring, cstring };
> const char * b[] = {
> bstring, astring, cstring, cstring };

well, i can live with it but the original idea was so much nicer

thanks for the answers

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-08-2007
Eric Sosman wrote:
>

.... snip ...
>
> Learn how your tools work. Then use them if they work well enough
> for your purposes, or abandon them and choose other tools if not.
> Don't waste time whining that your Phillips screwdriver doesn't
> turn Torx bolts.


Well, they might if you heat the bits up to a dull red, hammer them
into a Torx head, remove and heat-treat. By then it probably
won't handle Phillips heads, though.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-08-2007
CBFalconer wrote:
> Eric Sosman wrote:
> ... snip ...
>> Learn how your tools work. Then use them if they work well enough
>> for your purposes, or abandon them and choose other tools if not.
>> Don't waste time whining that your Phillips screwdriver doesn't
>> turn Torx bolts.

>
> Well, they might if you heat the bits up to a dull red, hammer them
> into a Torx head, remove and heat-treat. By then it probably
> won't handle Phillips heads, though.


Damn, you're right! I've seen other programmers do that;
worse, I've done it myself ...

"When your favorite tool is a hammer, every problem looks
like a nail."

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      11-08-2007
Eric Sosman wrote:
> Damn, you're right! I've seen other programmers do that;
> worse, I've done it myself ...
>
> "When your favorite tool is a hammer, every problem looks
> like a nail."


"When your favourite tool is a Java-shaped hammer, every problem looks
like a screw..."


Phil


PS I don't really dislike Java, but it's not as fun to poke fun at
genuinely useless languages like COBOL...

--
Philip Potter pgp <at> doc.ic.ac.uk
 
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
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments