Hi, can someone please tell me why I cant define this as a global variable? And if there is some other way to do it...
Code:char scherm[35][67];
Hi, can someone please tell me why I cant define this as a global variable? And if there is some other way to do it...
Code:char scherm[35][67];
What do you mean "you can't"? Have you tried or did you just hear somewhere that you can't? If you've tried, do you get an error? What does the error say? What operating system are you using? What compiler?
It's easier for us to help you if you tell us what it is doing.
There's no "C says you can't define a multidimensional array globally" if that's what you're looking for.
You might want to give this webpage a quick glance: http://www.catb.org/~esr/faqs/smart-questions.html
If you understand what you're doing, you're not learning anything.
Ok, I thought the error was common, but it seems not to be the case, so here is some more info:
I try to define a global two dimensional array called "screen" with f.e. 5 rows and 10 columns:
when I try to compile my program using the build in Bloodshed Dev-C++ compiler, under Windows XP pro, it states the following error:Code:char screen[5][10];
excuse me if my questions are starting to bug you...Code:variable-size type declared outside of any function
Last edited by LowLife; 11-18-2005 at 02:27 PM.
Are you really you're giving constants for the dimension sizes? Or are you doing something more like:
Please post the exact line of code that's causing the error.Code:char screen[height][width];
If you understand what you're doing, you're not learning anything.
You must not have been doing that exact line of code. Something like:is what it sounds like you were trying to do. The error you have is saying you're trying to declare a global array whose size is not known at compile time.Code:char screen[5][n];
Does this compile:It should, with the possible warning about an unused variable. Other than that, it should compile error free.Code:char foo[5][10]; int main( void ) { return 0; }
Quzah.
Hope is the first step on the road to disappointment.
indeed, it's not the exact code, guess I'm trying to hard to keep my explanation simple, my apologies, this is what I really did:
maybe this could solve it?Code:int row = 5, column = 10; char screen[row][column];
Code:#define row 5 #define column 10 char screen[row][column];
Did you compile and test this? As this would workOriginally Posted by LowLife
You can't declare an array's size outside of main with non-const variables. This is because, as Quzah said, you're trying to declare a global array whose size is not known at compile time.
Yes, your #define code would work and so would this.
Code:const int row = 5; const int column = 10; char screen[row][column];
Sent from my iPadŽ
I'm sticking with my #define code, since the thing the last post suggested:
gives the same error: "variable-size type declared outside of any function"Code:const int row = 5; const int column = 10; char screen[row][column];
Hmm... not on my compiler.
Sent from my iPadŽ
Then you should try compiling your C code as C instead of C++. const doesn't mean the same thing in both languages.Originally Posted by SlyMaelstrom
If you understand what you're doing, you're not learning anything.
Ah, good call.
Sent from my iPadŽ
You can also use enums (my favorite):
Code:enum { SIZE = 100 }; int array[SIZE];
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.