Gcc Error Variable-size Type Declared Outside Of Any Function
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up variable-size type declared outside of any function up vote 1 down vote favorite when declaring the two dimensional array int random[height][width]; and then using it in a function void populate(int random[height][width], int x, int y) gives the error variable-size type declared outside of any function. I know I'm doing something wrong, and that its something small. I just have a bad memory... c++ compiler-construction function declare share|improve this question asked Sep 16 '09 at 19:29 Mike 3153925 add a comment| 5 Answers 5 active oldest votes up vote 4 down vote accepted I'm going to step up right now and tell you that multidimensional arrays are not worth the brain effort in C or C++. You're much better off using single-dimensional arrays (or, better yet, standard containers) and writing an indexing function: inline int index (int x, int y) { return x + y * width; } Now for your problem. C++ does not support C99 variable-length arrays. The compiler must know, at compile time, the size of the array. The following, for example, won't work. int dim = 4; int ar[dim]; If dim were const, it would work because the compiler would be able to tell exactly how wide ar should be (because the value of dim wouldn't change). This is probably the problem you're encountering. If you want to be able to change the size at compile-time, you'll need to do something more difficult, like write a templated reference. You can't use a pointer for multidimensional arrays because of the way they are laid out in C/C++. A templated example might look like the following aberration: template
Programming Boards C++ Programming variable-size type declared outside of any function Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: variable-size type declared outside of any function Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-23-2002 #1 Okiesmokie View Profile View Forum Posts Registered User Join Date Jan 2002 Posts 387 variable-size type declared outside of any function ^ that is the http://stackoverflow.com/questions/1435004/variable-size-type-declared-outside-of-any-function error i get ^ i am declaring 2 global variables: PHP Code: //GlobalVariables
i need these global because i have my program in all different functions, and they all must access the players[] array. Does http://cboard.cprogramming.com/cplusplus-programming/13701-variable-size-type-declared-outside-any-function.html anyone have any ideas about what i could do? [INFO] I am making a program that helps with tournaments ( Lets the user enter all the data and the program seeds them and displays pairings ) I am using the MinGW Compiler (with the command line) I am running Windows 98 SE, but in a couple minutes i will install Windows ME [/INFO] 03-23-2002 #2 Shiro View Profile View Forum Posts .... Join Date Aug 2001 Location Groningen (NL) Posts 2,380 >int MAXPLAYERS; >int players[MAXPLAYERS+1]; Is it valid to declare players like this? Shouldn't be the size of the array a constant? If you declare these variables global, then there available to all functions just by using their names. Code: file 1: int global_variable; void function (void) { global_variable = 1; } file 2: extern int global_variable; void other_function (void) { printf ("Value of global_variable is %d\n", global_variable); } 03-23-2002 #3 Unregistered Guest change this: Code: int MAXPLAYERS to this: Code: const int MAXPLAYERS = 100; //for example Also, why would you have a variable called MAXPLAYERS then add 1 to it for the array, shouldnt it be MAXPLAYERSMINUSONE :P 03-23-2002 #4 ustuzou View Profile View Forum Posts Registered User Join Date F
intMAXPLAYERS;
intplayers[MAXPLAYERS+1];
Post your question and get tips & solutions from a community of 418,553 IT Pros & Developers. It's quick & easy. Specifying global array size with const int P: n/a Bilgehan.Balban Hi, The following code: https://bytes.com/topic/c/answers/222781-specifying-global-array-size-const-int #include
Post your question and get tips & solutions from a community of 418,553 IT Pros & Developers. It's quick & easy. Referencing a global array outside a function P: n/a flipflop I need to create a global array whose dimensions depend on the contents of another global array populated at its initialisation. For example: int array1[3]={3,2,1}; int array2[array1[0]]; //should be equiv. to: int array2[3]; GCC gives these errors: variable-size type declared outside of any function variable-sized object may not be initialized warning: excess elements in array initializer warning: (near initialization for `array2') Both arrays need to be global and created before main() starts. Any ideas? Nov 14 '05 #1 Post Reply Share this Question 12 Replies P: n/a Leor Zolman On 8 May 2004 07:21:09 -0700, fl******@easy.com (flipflop) wrote: I need to create a global array whose dimensions depend on thecontents of another global array populated at its initialisation. Forexample:int array1[3]={3,2,1};int array2[array1[0]]; //should be equiv. to: int array2[3];GCC gives these errors:variable-size type declared outside of any functionvariable-sized object may not be initializedwarning: excess elements in array initializerwarning: (near initialization for `array2')Both arrays need to be global and created before main() starts. Anyideas? #include