Global array - size

This is a discussion on Global array - size within the C Programming forums, part of the General Programming Boards category; How can I declare a global array whose size I calculate in a function later on (based on # of ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you need to use malloc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    22,181
    The tutorial on pointers has something to say about malloc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08: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, 03: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, 09:16 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21