tags:

views:

104

answers:

4

Compile error in vs2010(Win32 Console Application Template) for the code below. How can I fix it.

unsigned long long int Fibonacci[numFibs]; // error occurred here

error C2057: expected constant expression

error C2466: cannot allocate an array of constant size 0

error C2133: 'Fibonacci' : unknown size

Complete code attached(It's a sample code from programming In c -3E book. No any modify)

int main()
{

 int i, numFibs;

 printf("How may Fibonacci numbers do you want (between 1 to 75)? ");
 scanf("%i", &numFibs);

 if ( numFibs < 1 || numFibs > 75){
  printf("Bad number, sorry!\n");
  return 1;
 }

 unsigned long long int Fibonacci[numFibs];

 Fibonacci[0] = 0; // by definition
 Fibonacci[1] = 1; // ditto

 for ( i = 2; i < numFibs; ++i)
  Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1];

 for ( i = 0; i < numFibs; ++i)
  printf("%11u",Fibonacci[i]);

 printf("\n");

 return 0;

}
+1  A: 

You cannot have an array of variable length. Use dynamic allocation returning pointer (malloc)

Pavel Radzivilovsky
This is actually incorrect; C99 does support variable length arrays.
tzaman
Well, it's both. The questioner didn't specify which standard. When people talk about standard C, we're usually talking about the good C 89, unless otherwise specified. Although, maybe you could argue it was implied because the OP used the C++ style comments. My advice, stick with C89 and prefer malloc for this sort of thing.
BobbyShaftoe
+2  A: 

Either dynamically allocate the array or use a constant array size.

#define kMaxFibs 75
...
if ( numFibs < 1 || numFibs > kMaxFibs){
...
unsigned long long int Fibonacci[kMaxFibs];
drawnonward
@drawnonward, I'd like to use vs2010 for my dev ide. After followed your suggestion, it works quite well now.@All, Thank you.
Nano HE
+2  A: 

VS2010 is a C++ compiler by default, and C++ does not support variable length arrays. You can change your project to compile code as C code, but VS2010 still doesn't really support C99.

I would recommend you use gcc for C code, it's much more conformant.

GMan
+1  A: 

Which compiler are you using? And are you compiling as C or C++? Variable-length arrays are legal in C since C99, but if you're using an older compiler (or compiling as C++) they won't work.

As a workaround, you can switch to using dynamic allocation:

typedef unsigned long long uint64; // just for convenience
uint64* Fibonacci = (uint64*)malloc(sizeof(uint64)*numFibs);
// {code here}
// then at the end:    
free(Fibonacci);
return 0;
tzaman
VS2010. I created a win32 console application for practicing the code. I guess I compiled it as C++ application.
Nano HE
Yeah, VS isn't the really best option for C programming. You can try getting minGW/msys to use gcc..
tzaman
Thanks for the details code stuff and IDE suggestion.
Nano HE
@Nano HE : Thanks! Note that while the answer you accepted is fine, using a `#define` means you won't be able to select how many numbers to generate at run-time.
tzaman
@tzaman, I keep on study your code and I pasted to http://codepad.org/XuoHCP5T , It doesn't work. Could you please have a look?
Nano HE
@Nano - you need to replace the original Fibonacci declaration with the one I provided. Also, `scanf` won't work on codepad, I think. Here: http://codepad.org/IsECBa4A
tzaman
Understood. Thanks a lot.
Nano HE