Article ID: 142493 - Last Review: July 5, 2005 - Revision: 1.1

BUG: Cannot Cast Double to int for Use in an Array Declarator

This article was previously published under Q142493

On This Page

Expand all | Collapse all

SYMPTOMS

The compiler generates the following error when you type cast a 'const float' or 'const double' to 'const int' in an array declarator:
error C2057: expected constant expression

RESOLUTION

To work around this problem, use a #defined double value in place of a const double. See the "Workaround Sample Code" section for an example.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Sample Code to Demonstrate Problem

   /* Compile options needed: /c
   */ 

   const double d_max = 5.55;
   int arr[(const int) d_max];   // Gives C2057 here

   // The following code also gives the error:
   const double d_max = 5.55;
   const int imax = (const int) d_max;

   int arr[imax];   // Gives C2057 here
				

Workaround One - Sample Code

   #define D_MAX 5.55
   const int imax = (const int) D_MAX;
   int arr[imax];
				

Workaround Two - Sample Code

   #define D_MAX 5.55
   int arr[(const int) D_MAX];
				

APPLIES TO
  • Microsoft Visual C++ 4.0 Professional Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 5.0 Learning Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
Keywords: 
kbbug kbcompiler kbcpponly KB142493
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations