Thread: global variables and multidimensional arrays

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    global variables and multidimensional arrays

    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];

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    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:

    Code:
    char screen[5][10];
    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:
    variable-size type declared outside of any function
    excuse me if my questions are starting to bug you...
    Last edited by LowLife; 11-18-2005 at 02:27 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you really you're giving constants for the dimension sizes? Or are you doing something more like:
    Code:
    char screen[height][width];
    Please post the exact line of code that's causing the error.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You must not have been doing that exact line of code. Something like:
    Code:
    char screen[5][n];
    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.

    Does this compile:
    Code:
    char foo[5][10];
    int main( void )
    {
        return 0;
    }
    It should, with the possible warning about an unused variable. Other than that, it should compile error free.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    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:

    Code:
    int row = 5,
        column = 10;
    char screen[row][column];
    maybe this could solve it?

    Code:
    #define row 5
    #define column 10
    char screen[row][column];

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by LowLife
    maybe this could solve it?
    Did you compile and test this? As this would work

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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Ž

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    I'm sticking with my #define code, since the thing the last post suggested:

    Code:
    const int row = 5;
    const int column = 10;
    char screen[row][column];
    gives the same error: "variable-size type declared outside of any function"

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... not on my compiler.
    Sent from my iPadŽ

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by SlyMaelstrom
    Hmm... not on my compiler.
    Then you should try compiling your C code as C instead of C++. const doesn't mean the same thing in both languages.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah, good call.
    Sent from my iPadŽ

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM