Variables in array declarations

This is a discussion on Variables in array declarations within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with an error on a line where I'm trying to declare an array. Boiled down, my ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Variables in array declarations

    I'm having a problem with an error on a line where I'm trying to declare an array. Boiled down, my code looks like this:
    Code:
    typedef struct
    {
    	...
    	int width, height;
            ...
    } BMP_INFOHEADER;
    
    int main ()
    {
            BMP_INFOHEADER infohead;
    
            ...    // infohead gets filled in here
    
            char ascii [infohead.height][infohead.width];   // error!
    
            ...
    }
    The error I get on that line looks is
    "illegal constant expression"

    I assume it has something to do with the variables in the [][]'s. I thought I had an idea how to fix it, but I was wrong.

    What's going on?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't use non-constant expressions in array definitions. You must use dynamic allocation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    constant variables actually mean that the compiler knows them at compile time. wich your infoheader is in memory and is not know by the compiler so the compiler doesnt know what todo.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use a vector or boost::array so you don't have to manage the dynamic memory yourself.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Okay, dynamic allocation. Does that mean doing something like this?
    Code:
    {
            ...
            char * ascii;
            ascii = new char [infohead.height][infohead.width];
            ...
    Because that gives me the same error.

    Also, that change brings up other errors later in my code
    Code:
    {
            char colors [] = { 'f', 'o', '0' };
            ...
            // ERROR: pointer/array required
            ascii[i][j] = colors[c];
            
            // same error
            fputc (array[i][j], file);
    }

  6. #6
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    Code:
    ascii = new char [infohead.height + 1][infohead.width + 1];
    try that

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    You can't directly allocate a 2D array using operator new. A couple of choices (with different trade-offs) are;
    Code:
      char **ascii;
      ascii = new (char *) [infohead.height];
      for (int i = 0; i < info.height; ++i)
          ascii[i] = new char[infohead.width];
      // can now access elements via  ascii[i][j] where i and j are integers
    or
    Code:
      char *ascii;
      ascii = new [infohead.height *infohead.width];
    
      // can now access elements via  ascii[i*infohead.height + j] where i and j are integers
    Youy might also want to consider using a std::vector<std::vector<char> > or a std::vector<std::string> depending on what you're trying to do....

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or a boost::multi_array. http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM

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