473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
3 2200
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
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
thank you both. Luke, thanks much for the detailed explanation...you
made it easy to understand.

May 19 '06 #4

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

Similar topics

5
by: F. Da Costa | last post by:
Hi, Could it be correct that the following code does *not* work because i'm not using the var arr = new Array("a","b","c"); methodology?? Read through...
3
by: Debashish Chakravarty | last post by:
Hi, I have a file with a single column containing observations of an experiment, the number of observations is not known in advance and I have to read them all into an array, I am using realloc...
6
by: shawn | last post by:
test1.c ------------------------------ #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { const int MAX_CHAR_NUM=10; char name="Computer"; printf("My name is %s.\n",...
11
by: shan | last post by:
how to declare a variable that should contain both alpahabets and numbers.It should not be an string or char array.
11
by: Hipo | last post by:
Hi. I have the following code: unsigned char temporary; unsigned __int64 *reader = (unsigned __int64*)&temporary+20; std::cout << *reader-- << std::endl; std::cout << *reader-- << ...
4
by: codergem | last post by:
Helo friends Could any of you please help me out with this problem. To me this program of heap sort seems logically perfect but still the output of this is not coming right, I dont want to follow...
7
by: Michael | last post by:
Hi, I am trying to delete p to see whether the thing pointed toby p is deleted. However, the program is stuck there. Could you please help me out? Thanks in advance, Michael #include...
6
by: paul.anderson | last post by:
This code doesn't work - the first retrieval of t2 returns valid data, the subsequent do not. Please help!!! int main(int argc, char* argc){ struct Test{ int i; int j; intk; int l; }
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.