array errors

This is a discussion on array errors within the C++ Programming forums, part of the General Programming Boards category; Basically this program just reads in a seating chart... Problem is I keep getting the error below... Im not sure ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    array errors

    Basically this program just reads in a seating chart... Problem is I keep getting the error below... Im not sure what it means... My code seems correct?.. any help?

    getting error

    Code:
    main.cxx:8: error: variable-size type declared outside of any function
    main.cxx:8: error: variable-size type declared outside of any function
    main.cxx:10: error: variable-size type declared outside of any function
    main.cxx:11: error: variable-size type declared outside of any function
    main.cxx:33: error: variable-size type declared outside of any function
    main.cxx:40: error: variable-size type declared outside of any function
    Main.cxx
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int SIZE = 50;
    int num_mat,occupied, row, col;
    char m1[SIZE][SIZE];
    
    void init_array(char array[][SIZE]);
    void display_array(char array[][SIZE]);
    
    int main() {
    init_array(m1);
    
    cin >> num_mat;
    cin >> occupied;
    
    while( !cin.eof() ) {
       cin >> row >> col;
       if(col=='\n') {
          occupied = row;
          display_array(m1);
          init_array(m1);
       } else
             m1[row][col] = 'X';
    
    }
    
    return 0;
    }
    
    void init_array(char array[][SIZE]) {
       for(int i=0;i<SIZE;i++) {
          for(int j=0;j<SIZE;j++)
             array[i][j] = 'E';
       }
    }
    
    void display_array(char array[][SIZE]) {
        for(int i=0;i<SIZE;i++) {
          for(int j=0;j<SIZE;j++)
             cout << array[i][j] << endl;
       }
    }

    Thanks for any assistance guys.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Array declarations use constant expressions for subscripts. For example:
    Code:
    D1[constant-expression]
    If the constant-expression is present, it must have integral type, and value greater than 0.

    A constant expression is an expression that involves only constants. Such expressions may be evaluated during compilation rather than run-time, and accordingly may be used in any place that a constant can occur. For example:
    Code:
    #define SIZE 50
    char line[SIZE+1];
    An array's subscript must always be a constant expression. In your example, size is defined during run-time rather than a constant.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to make your array dimensions constant. Right now it is

    Code:
    int SIZE = 50;
    So just change that to:

    Code:
    const int SIZE = 50;
    and it should compile fine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    const int SIZE did it..

    thank you gentlemen :-P.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

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