[Newbie] Const and Array

C

Cadderly

Hello,

I wonder why my compiler (Bcc55) complains about this one saying:
Error E2313 temp.c 6: Constant expression required in function main

int main(void)
{
const int BUF = 100;
char bufarr[BUF];

return 0;
}
 
M

Matt Gregory

Cadderly said:
Hello,

I wonder why my compiler (Bcc55) complains about this one saying:
Error E2313 temp.c 6: Constant expression required in function main

int main(void)
{
const int BUF = 100;
char bufarr[BUF];

return 0;
}

Basically, BUF is assigned at run time, and the compiler needs to know
the size of bufarr at compile time. You have to use a #define or an
enumeration for array indexes.
 
M

Mark Gordon

Hello,

I wonder why my compiler (Bcc55) complains about this one saying:
Error E2313 temp.c 6: Constant expression required in function main

int main(void)
{
const int BUF = 100;
char bufarr[BUF];

return 0;
}

It's quite simple. Despite const being an abbreviation of constant it
does not really declare constants in C. If you use
enum { BUF=100 };
then you will actually have a constant which follows the expected
scoping rules, alternatively use a #define.
 
C

Cadderly

Basically, BUF is assigned at run time, and the compiler needs to know
the size of bufarr at compile time. You have to use a #define or an
enumeration for array indexes.

Thanks, I know that the compiler needs to know the size of the array at
compile time but why BUF is assigned at run time? Since BUF is a constant
what's the point?
 
I

Irrwahn Grausewitz

Thanks, I know that the compiler needs to know the size of the array at
compile time but why BUF is assigned at run time? Since BUF is a constant
what's the point?
From your point of view

const int BUF = 100;

defines an integer constant. From C's (and therewith your
compiler's) POV, BUF is an integer variable whos value cannot
be changed. And variables (const or not) are evaluated at run
time. Looks strange to beginners, but that's the way C deals
with it...

Irrwahn
 
M

Matt Gregory

Cadderly said:
Thanks, I know that the compiler needs to know the size of the array at
compile time but why BUF is assigned at run time? Since BUF is a constant
what's the point?

Well, you can use a function to initialize it. I think the point
is to keep the language simple by not treating literals as exceptions
to the rule.
 
M

Martin Dickopp

Cadderly said:
int main(void)
{
const int BUF = 100;
char bufarr[BUF];

return 0;
} [...]
Since BUF is a constant what's the point?

`BUF' is not a constant, `BUF' is a const-qualified variable.
IOW, a variable which cannot be modified, but still a variable.

Martin
 
M

Matt Gregory

I said:
Well, you can use a function to initialize it. I think the point
is to keep the language simple by not treating literals as exceptions
to the rule.

Plus there's the point that since you declared BUF inside main(),
that piece of memory doesn't exist until execution enters main().
 
A

Al Bowers

Cadderly said:
Thanks, I know that the compiler needs to know the size of the array at
compile time but why BUF is assigned at run time? Since BUF is a constant
what's the point?

C language now defines the variable length array (vla) type. You
will have to upgrade your compiler to a version that supports the
current C Standard, or at least supports vla's. For example, the
Standard now supports the following vla example:

#include <stdio.h>

int main(void)
{
int n = 5,i;
int array[n];

for(i = 0; i < 5;i ++)
{
array = i;
printf("array[%d] = %d\n",i,array);
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top