By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

what is wrong with this code: declaring a char array using variable

P: n/a
i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];

May 18 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
fa*****@optonline.net wrote:
i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];


The array size (fSize) must be a compile time constant. If not you need
an alternate approach such as dynamic allocation (via new[]) or a vector.

Mark
May 19 '06 #2

P: n/a
Mark P wrote:
fa*****@optonline.net wrote:
i am getting "error C2057: expected constant expression" with the
following code:

ifstream f( argv[ 1 ] );
f.seekg( 0, ios::end );
const long fSize = f.tellg();
f.close();

char content[ fSize ];


The array size (fSize) must be a compile time constant. If not you need
an alternate approach such as dynamic allocation (via new[]) or a vector.


To give a little more exposition (in case you want it)...

Mark is completely correct -- the compiler distinguishes between values
which are compile-time constants, and those which are not. That means
that the value is known at compile time, so it's information available
to the compiler. Constrast this with other types of values, where the
only thing the compiler knows is the data type, not the value.

Ways to get a compile-time constant include:
* Use a numeric literal
* Use the sizeof operator
* Combine other compile-time constants, using operators (e.g.
sizeof(int) << 8). This includes arithmetic, boolean, shifting,
ternary, etc.
* Use an enum value
* Use a boolean literal

These are some of the things which you can do with compile-time
constants, but not with other kinds of values:
* Specify the size of a stack-allocated array (this is what you're
trying to do above)
* Instantiate a template with a non-type parameter (e.g. template <int
i> class pow_)
* Combine them to form other compile-time constants
* Perform static assertions (conditions checked at compile time)

Now, one more thing you may be wondering -- why can't you do what you
tried to do? There are various levels of complexity with which one
could answer this question -- I'll do my best to strike a balance.
Arrays which are not created using dynamic memory allocation (new,
malloc, etc.) are allocated on the stack. When the compiler creates
the assembly code associated with a function or other scope, it needs
to know at compile time how much memory will be used by all the local
variables. The amount of memory depends on the size of the array,
obviously, so you can only use a size that is known at the time when
the compiler needs to make this decision -- that is, a compile-time
constant. Make more sense now?

Luke

May 19 '06 #3

P: n/a
thank you both. Luke, thanks much for the detailed explanation...you
made it easy to understand.

May 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.