#### Archived

This topic is now archived and is closed to further replies.

# expected constant expression error

This topic is 5677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I can''t figure something out, i.e. don''t know why I get this error. I will explain what I''m doing: I have a function LoadTextures(char *textureFiles[],int numTextures) This function I use to initialize the textures i.e. generate them with glGenTextures. So what I need is an array of the form GLUint textures[numTextures]; LoadTextures is called from the main Initialization function which is found in another file. So VC++ gives me following error: D:\Programming\NeHeGL\init.cpp(33) : error C2057: expected constant expression D:\Programming\NeHeGL\init.cpp(33) : error C2466: cannot allocate an array of constant size 0 That is I can''t use the "int numTextures" for the length of the "textures" array. In fact, when I put a constant in it everything works fine. However, as I do not know the number of textures that will be loaded, I certainly can''t hard Code it. So does anyone know why it does that. For testing purposes I did the exact same thing with a very simple function and it worked

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

void test(int i){
int j = 0;
int test[i];
for (j = 0; j<i;j++){
test[j] = j;
printf("%d\n", test[j]);
}
}

int main(int argc, char *argv[])
{
int i = 10;
test(i);
system("PAUSE");
return 0;
}

However, it doesn''t seem to work with my more complex code

##### Share on other sites
Some compilers aren't very ANSI-C/C++ compatible (MSVC V6 especially) and won't like the:

int test [ i ];

construct, although, I believe, gcc copes OK with that one. You'll need to use new and create a dynamic array, thus:

int *a_test = new int [ i ];

which would be more compatible and won't cause you to run out of heap space. Don't forget to make sure you delete it:

delete [] a_test;

[Personal habit: I prefix dynamically allocated array names with 'a_' so that determining the correct delete to use is simple.]

Skizz

[edited by - Skizz on January 31, 2003 12:50:22 PM]

[edited by - Skizz on January 31, 2003 12:50:48 PM]

##### Share on other sites
Thank you for the fast reply. It works that way.

You''re right about gcc. The code from the test example has been compiled using gcc on my other Box. Whereas the original error came from VC++ 6.0.

##### Share on other sites
quote:
Original post by Skizz
Some compilers aren''t very ANSI-C/C++ compatible (MSVC V6 especially) and won''t like the:

int test [ i ];

That''s not ANSI/ISO C++: you can not define an array of non-constant length; any compiler that allows you to is providing compiler-specific extensions.

Otherwise, of course, you''re absolutely right

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 83
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004412

×