473,698 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constants in array size

I have the following code:

const int num_segments = 16;

int some_function(v oid)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.

--
Gopi Sundaram
go*****@cse.sc. edu
Nov 14 '05 #1
4 2650
Gopi Sundaram wrote:
I have the following code:

const int num_segments = 16;

int some_function(v oid)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.


switch will also complain if you use const ints to match against ints. This is
really annoying.
Nov 14 '05 #2
Peter Hickman wrote:
Gopi Sundaram wrote:
I have the following code:

const int num_segments = 16;

int some_function(v oid)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.

`const' does not mean "constant." What it means is
something more like "read-only." `num_segments' is not
a constant, but a variable -- a read-only variable, but
a variable nonetheless.
switch will also complain if you use const ints to match against ints. This is
really annoying.


Same reason.

Non-constant `const' looks pretty silly in contexts
like the above, but consider this one:

extern const int num_segments;
...
int key[num_segments];

As before, `num_segments' is a read-only variable, but
it is defined in some other module. The compiler cannot
"see" that other module while compiling this one, so it
cannot know what the variable's value might be. In fact,
there might be seven different definitions of `num_segments'
in seven different source files; the compiler has no way
of knowing which of those sources will eventually be linked
into the same program with this one.

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

Nov 14 '05 #3
On Mon, 22 Nov 2004 11:19:55 -0600, Gopi Sundaram <go*****@cs.sc. edu>
wrote in comp.lang.c:
I have the following code:

const int num_segments = 16;

int some_function(v oid)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size array",
but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration is
legal ANSI C.


The declaration is legal in a compiler that conforms with the updated
1999 version of the C language standard, although such compilers are
rather few and far between, and the conformance of most of them is not
complete. The error message you cite seems to indicate that your
compiler does not conform to C99, or is not being invoked in a C99
conforming mode, but rather accepts this declaration as a non-standard
extension of its own.

Under C99 automatic arrays, those defined inside functions without use
of the 'static' keyword, can have variable sizes so long as the size
is greater than 0.

It is illegal under the original ANSI 1989 and ISO 1990 standards.

Prior to C99, and for arrays with static storage duration even under
C99, the size of an array must be specified as a compile-time constant
expression. The value of an object, even a const qualified object, is
not a compile-time constant expression under any version of C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
On Mon, 22 Nov 2004 12:39:32 -0500
Eric Sosman <er*********@su n.com> wrote:
Peter Hickman wrote:
Gopi Sundaram wrote:
I have the following code:

const int num_segments = 16;

int some_function(v oid)
{
int key[num_segments + 2];

...
}

My compiler barfs out the warning "ANSI C forbids variable-size
array", but lets it pass.

Does an expression using a const-variable count for the purposes of
"variable-size array"? I was under the impression that declaration
is legal ANSI C.


`const' does not mean "constant." What it means is
something more like "read-only." `num_segments' is not
a constant, but a variable -- a read-only variable, but
a variable nonetheless.


<snip>

The OP can get what he probably wants by using:

enum { NUM_SEGMENTS = 16 };

Unlike a #define, the enum follows the same scoping rules as variables.
I.e. if you do it inside a function it will only exist inside the
function.

On the other hand, if you do
#define NUM_SEGMENTS 16

then NUM_SEGMENTS will exist from that line until the end of the file or
you do something explicit to change it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5

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

Similar topics

4
3687
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array of the following kind: $people = array(); // maps age and e-mail address to names $people = array(21, "paul@foo.bar"); $people = array(22, "linda@bar.foo"); $people = array(19, "max@foobar.foobar");
9
1587
by: john smith | last post by:
Hi, If I have a large constants file and include that in a .cpp file will the executable become large? That is if I include a file with a bunch of constants does the executable include all of the constants or just the constants that are used in the .cpp file? I am wanting a way so that only the constants that are used are included in
27
2041
by: Peter Ammon | last post by:
My code obfuscator gave me this: char buff; to which gcc retorted: "ISO C90 forbids variable-size array 'buff'" and checking the standard, it appears that commas are indeed forbidden from being in a constant expression.
8
3103
by: gthorpe | last post by:
Hi, I have a question about string constants. I compile the following program: #include <stdio.h> #include <string.h> int main(void) { char str1 = "\007";
34
3371
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const long double const_pi=0.0; lines to // rodata
6
3125
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. That seems good.
7
11372
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I haven't found a way to do this. Is there one? Thanks, Gary
4
1394
by: amun25dringer11 | last post by:
I hear people talking about like: const float PI=3.14; that and they say you can only change it in one place but why do you need it if you can use it in a variable ex. float PI=3.14; and then I can assign it to another float; for(int pi=PI;;pi++) and if you change the value of PI then the value of pi will change as
54
3598
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
0
9157
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
9026
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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...
1
6518
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.