Home > Gcc Error > Gcc Error Variable-size Type Declared Outside Of Any Function

Gcc Error Variable-size Type Declared Outside Of Any Function

Anyideas? #include #define arrsize(a) (sizeof(a) / sizeof(*a)) int array1[3]={3,2,1}; int array2[arrsize(array1)]; /*should be equiv. If the array has extern scope, the array size had also better have extern scope. You'd have use constants or allocate it dynamically (either in heap or in the stack frame). If the array has extern scope, the array size had also better have extern scope. this content

Note You need to log in before you can comment on or make changes to this bug. It looks like what you're trying to do is create an array that changes in size, and as far as I know, that is illegal in C++. I know I'm doing something wrong, and that its something small. Save your draft before refreshing this page.Submit any pending changes before refreshing this page.

Hurkyl, Sep 23, 2011 Sep 23, 2011 #8 uart Science Advisor Hurkyl said: ↑ Try making it static. Read these ‪AR‬-related sites to keep up with the industryLearn More at Metavision.comAnswer Wiki2 Answers Jaskaran Virdi, Graduate Student in Computer Science at University of Calfornia, San DiegoWritten 93w agoA2A.You've incorrectly Anyideas? #include #define arrsize(a) (sizeof(a) / sizeof(*a)) int array1[3]={3,2,1}; int array2[arrsize(array1)]; /*should be equiv.

Home | New | Search | [?] | Reports | Requests | Help | NewAccount | Log In [x] | Forgot Password Login: [x] | Report Bugzilla Bug Legal Forums Search Yup; a case of overloaded 3's ;-) Thanks, -leor The answer is: it cannot be done directly. Myers 2009-04-01 14:46:39 UTC Fixed on trunk for 4.5. If you post code to this newsgroup, *please* cut-and-paste the actual code that you fed to the compiler; don't try to re-type it.

Forum Today's Posts C and C++ FAQ Forum Actions Mark Forums Read Quick Links View Forum Leaders What's New? BTW. You can't use variable length arrays at file scope or as an element of a structure. http://cboard.cprogramming.com/cplusplus-programming/13701-variable-size-type-declared-outside-any-function.html i.e.

Anyhow, the problem here is that 'const' doesn't quite mean 'const' in C. Bug24507 - Compiler error from variable-size type Summary: Compiler error from variable-size type Status: CLOSED RAWHIDE Aliases: None Product: Red Hat Linux Classification: Retired Component: gcc (Show other bugs) Sub Component: Yes, my password is: Forgot your password? Sep 23, 2011 #1 uart Science Advisor I know that it's often considered better practice to use C's "typed constants" instead of "#define" as it may offer better error/type checking.

As I said before, you can't have an unspecified array size upon declaration. Also, are you compiling to the C99 standard, or the older one? I don't think that's what the OP wanted (see underlined text). `array2' shouldn't have 3 elements because `array1' has three elements, but because the zeroth element of `array1' is 3. Note also that VLAs are a new feature in C99, and some compilers may not yet implement them.

What is so gosh-darn important about having to "extract it out of the array", anyway? news As you've seen, if these declarations appeared outside a function, the declaration of arr1 would be illegal, simply because you can't have global VLAs. Note that today's implementations of VLA are broken, even with gcc 4.x). Martin -- ,--.

It should really be called something like "readonly". C++ does not support C99 variable-length arrays. For run-time, you'll need to use new to allocate data, or use a container type. http://blogeurope.net/gcc-error/gcc-error-xitwas-not-declared-in-this-scope.php So the compiler differentiates between a literal and a const int when they're declared global, but make no distinction when they're local.

Comment 2 Jakub Jelinek 2001-02-05 16:08:30 EST Fixed in gcc-2.96-74. Is the NHS wrong about passwords? share|improve this answer answered Sep 16 '09 at 19:35 Michael Krelin - hacker 65k6131141 add a comment| up vote 1 down vote When an array is passed directly as parameter to

In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers see an example newsletter By subscribing, you agree to the privacy policy and terms

I believe this is a consequence of the following patch. 2009-03-29 Joseph Myers PR c/456 PR c/5675 PR c/19976 PR c/29116 PR c/31871 PR c/35198 Comment 1 Joseph S. I probably should have asked the question more simply, as: " How can you reference a global array element, set during the array's initialisation, before main() starts? However if I scrap the typedef (but leave the const defined there) and then place the following in the main program, Code (Text): const int ListSize = 80; ... One possibility is to define a macro which expands to `3', and use it to both initialize the zeroth element of `array1' and define the size of `array2'.

I probably should have asked thequestion more simply, as:" How can you reference a global array element, set during thearray's initialisation, before main() starts? The time now is 08:34 PM. This is a compile-time constant to both C and C++. check my blog phinds, Sep 23, 2011 Sep 23, 2011 #7 Hurkyl Staff Emeritus Science Advisor Gold Member uart said: ↑ The latter doesn't compile in standard C as it doesn't seem to recognize

It's quick & easy. to: int array2[3]; #define ARRAY1_MAX (3) #define ARRAY1(n) (ARRAY1_MAX - (n)) int array1[3] = { ARRAY1(0), ARRAY1(1), ARRAY1(2) }; int array2[ARRAY1(0)]; -- James Nov 14 '05 #3 P: n/a Jeremy Yallop This is probably the problem you're encountering.