Thread: Global array - size

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Global array - size

    How can I declare a global array whose size I calculate in a function later on (based on # of rows & columns of terminal ).

    Code:
    // global vars
    int row, col;
    char box[row][col];  // causes error
    
    ...
    
    void getsize(){
    isatty(STDIN_FILENO);
    struct winsize sz;
    ioctl(STDIN_FILENO,TIOCGWINSZ,&sz);
    row=sz.ws_row;
    col=sz.ws_col;
    }
    Error I am getting: variably modified 'box' at file scope

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to use malloc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    How can I do this?

    Or could someone please point me to a good tutorial.

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The tutorial on pointers has something to say about malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I wasn't sure myself, based on that tutorial I'd say malloc a size to a created pointer and then use that pointer to point to the rows and columns

    int *ptrRow = malloc (sizeof(int));
    int *ptrCol = malloc (sizeof(int));
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not right.

    A 2D array allocation needs one malloc, then a loop to malloc memory for the other dimension:
    Code:
    int **ptr;
    int i;
    ptr = malloc(sizeof(*ptr) * rows);
    if (!ptr) allocerror();
    for(i = 0; i < rows; i++)
    {
       ptr[i] = malloc(sizeof(int) * cols);
       if (!ptr[i]) allocerror();
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by matsp View Post
    Not right.

    A 2D array allocation needs one malloc, then a loop to malloc memory for the other dimension:
    Code:
    int **ptr;
    int i;
    ptr = malloc(sizeof(*ptr) * rows);
    if (!ptr) allocerror();
    for(i = 0; i < rows; i++)
    {
       ptr[i] = malloc(sizeof(int) * cols);
       if (!ptr[i]) allocerror();
    }
    --
    Mats
    What's wrong with doing it my way? Then assigning values to the pointer after row and column have been determined?

    [*ptrRow][*ptrCol]
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But you are only allocating a single integer - in which case, 99% of the time, you can just use the ORIGINAL int value, instead of storing it in a pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Cool BIG thanks for the help!
    Last edited by Tupcia; 04-15-2008 at 03:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  3. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  4. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 08:16 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM