473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

const is not const

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?

Nov 7 '07 #1
11 1770
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
Nov 7 '07 #2
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
Nov 7 '07 #3
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.

--
Er*********@sun.com

Nov 7 '07 #4
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
Nov 7 '07 #5
Szabolcs Nagy <ns*******@gmail.comwrites:
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) ks***@mib.org <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"
Nov 7 '07 #6

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

Nov 7 '07 #7
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

Nov 8 '07 #8
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
es*****@ieee-dot-org.invalid
Nov 8 '07 #9
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 <atdoc.ic.ac.uk
Nov 8 '07 #10
On Nov 8, 4:36 am, Eric Sosman <Eric.Sos...@sun.comwrote:
Szabolcs Nagy wrote On 11/07/07 09:01,:
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? */
Well, initialized consts would be constant expressions,
and uninitialized ones wouldn't be. (NB. You might need
to language lawyer that sentence a bit to make it rigorous).

I don't see a great problem here; 'that other language'
follows this approach.

Nov 8 '07 #11
On Nov 8, 4:50 am, jacob navia <ja...@nospam.comwrote:
Andrey Tarasevich wrote:
Szabolcs Nagy wrote:
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.
In C99 this is a VLA ; you still can't declare a
'normal' array with a variable specifying the length.

Nov 8 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.