469,917 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Specifying global array size with const int

Hi,

The following code:

#include <stdio.h>

// const int const_asize = 10;
#define define_asize = 10;

int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};

int main(int argc, char * argv[])
{

return 0;
}

Using the #define compiles with no errors, however if you replace it
with the constant variable, I get the following error:

"error: variable-size type declared outside of any function"
"error: variable-sized object may not be initialized"

this is on gcc compiling with the following: gcc -o consttest main.c
adding a -std=c99 does not make any difference.

So the compiler differentiates between a literal and a const int when
they're declared global, but make no distinction when they're local. My
question is, is this standard C behaviour? i.e. Has it been defined as
part of C to refuse to compile above code with a const int specifying
array size, when the array is declared global?

Thanks,
Bahadir

Nov 15 '05 #1
4 21087
<Bi*************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
....
// const int const_asize = 10;
#define define_asize = 10;

int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};

....

IMO, what you're trying to do is C++, which is not C.

Alex
Nov 15 '05 #2
Bi*************@gmail.com wrote on 01/09/05 :
Hi,

The following code:

#include <stdio.h>

// const int const_asize = 10;
#define define_asize = 10;

int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};
You don't need the size. The compiler can count the initializers and
size the array for you :

int array[] = {1,2,3,4,5,6,7,8,9,0};

You can retreive the size by applying the definition of an array
(sequence of elements of the same size), hence :

size_t n = sizeof array / sizeof array[0];

or

size_t n = sizeof array / sizeof *array;

which is easily macroizable to the handy:

#define NELEM(a) (sizeof(a)/sizeof*(a))
int main(int argc, char * argv[])
{

return 0;
}

Using the #define compiles with no errors, however if you replace it
with the constant variable, I get the following error:

"error: variable-size type declared outside of any function"
"error: variable-sized object may not be initialized"
Yes. In C, the array sizer must be a constant expression
(C99 allows VLA under certain conditions. Note that today's
implementations of VLA are broken, even with gcc 4.x).
this is on gcc compiling with the following: gcc -o consttest main.c
adding a -std=c99 does not make any difference.
Obviously, VLA are not permitted on static arrays.
So the compiler differentiates between a literal and a const int when
they're declared global, but make no distinction when they're local. My
question is, is this standard C behaviour? i.e. Has it been defined as
part of C to refuse to compile above code with a const int specifying
array size, when the array is declared global?


Yes, it's conforming with C99.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #3
Bi*************@gmail.com writes:
The following code:

#include <stdio.h>

// const int const_asize = 10;
#define define_asize = 10;

int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};

int main(int argc, char * argv[])
{

return 0;
}

Using the #define compiles with no errors,
No, it doesn't, but if you change the line
#define define_asize = 10;
to
#define define_asize 10
it does compile.

This isn't just nitpicking. If you post code to this newsgroup,
*please* cut-and-paste the actual code that you fed to the compiler;
don't try to re-type it. The more time we spend trying to distinguish
between a possible typo and whatever is actually causing the problem
you're asking about, the less time we can spend answering your
question.

(Incidentally, it's traditional for macro names to be in all-caps,
e.g.:

#define DEFINE_ASIZE 10
int array[DEFINE_ASIZE] = {1,2,3,4,5,6,7,8,9,0};
however if you replace it
with the constant variable, I get the following error:

"error: variable-size type declared outside of any function"
"error: variable-sized object may not be initialized"

this is on gcc compiling with the following: gcc -o consttest main.c
adding a -std=c99 does not make any difference.
Right. The keyword "const" doesn't create a constant. It should
really be called something like "readonly". The way to create a true
constant is either to use #define, or, for a value that fits in an
int, an enum declaration:

enum { asize = 10 };
int array[asize] = {1,2,3,4,5,6,7,8,9,0};

The latter is probably an abuse of enum types, but it's a fairly
common idiom.

The lack of a good way to create genuine constants in C is arguably a
flaw in the language, but we're stuck with it.
So the compiler differentiates between a literal and a const int when
they're declared global, but make no distinction when they're local. My
question is, is this standard C behaviour? i.e. Has it been defined as
part of C to refuse to compile above code with a const int specifying
array size, when the array is declared global?


It does distinguish between a literal and a const int in either
context. The difference is that variable length arrays (VLAs) are
legal in a local context. In the following:

int main(void)
{
#define TEN 10
const int ten = 10;
int arr0[TEN];
int arr1[ten];
...
}

arr0 is an ordinary array, but arr1 is a VLA. As you've seen, if
these declarations appeared outside a function, the declaration of
arr1 would be illegal, simply because you can't have global VLAs.

Note also that VLAs are a new feature in C99, and some compilers may
not yet implement them.

Finally, as someone else has pointed out, you don't need to specify the
size in this case; the size can be inferred from the initialization:

int array[] = {1,2,3,4,5,6,7,8,9,0};

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4
Keith Thompson wrote:
It does distinguish between a literal and a const int in either
context. The difference is that variable length arrays (VLAs) are
legal in a local context. In the following:

int main(void)
{
#define TEN 10
const int ten = 10;
int arr0[TEN];
int arr1[ten];
...
}

arr0 is an ordinary array, but arr1 is a VLA.
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Thank you, this clarified it for me very well. I won't post broken code
next time.

Regards,
Bahadir

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Gianni Mariani | last post: by
7 posts views Thread by news.hku.hk | last post: by
12 posts views Thread by flipflop | last post: by
8 posts views Thread by chellappa | last post: by
9 posts views Thread by barcaroller | last post: by
1 post views Thread by asit | last post: by
10 posts views Thread by Chunekit Pong | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.