473,761 Members | 5,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1796
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*******@gmai l.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_Keit h) 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

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

Similar topics

8
2589
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
2507
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 keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
6
2423
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 implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
7
3803
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. And why would one choose one over the other. moreover, I thought there was a difference between the two below where one would not let the value change whereas the other one would not let the
3
2238
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. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
15
4189
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 MY_HEADER #define MY_HEADER const int FOO = 42;
4
2750
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
2790
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1875
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
4
6695
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.