Thread: [1][0]

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    [1][0]

    I've written this function to initialize a 2D array:
    Code:
    template<typename T, int iElements> void Initialize2D( T* Array[][iElements], int iElements1, int iElements2, T Value )
    {
      std::cout << "1"; // These are for debugging
      std::cin.get();
      for( int I = 0; I < iElements1; I++ )
      {
        std::cout << "I" << I;
        std::cin.get();
        for( int C = 0; C < iElements2; C++ )
        {
          std::cout << "C" << C;
          std::cin.get();
          *Array[I][C] = Value;
        }
      }
      std::cout << "2";
      std::cin.get();
    }
    I call it like this:
    Code:
    bool* Visited[10][10];
    Initialize2D<bool, 10>( Visited, 10, 10, false );
    The output:
    Code:
    I0
    C0
    C1
    C2
    C3
    C4
    C5
    C6
    C7
    C8
    C9
    I1
    C0
    *Crash*
    So, basically, *Array[1][0] = false is causing a crash. I have no idea why though. Any ideas?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think it should be like this.
    Code:
    #include <iostream>
    
    template<typename T, int iElements> 
    void Initialize2D( T Array[][iElements], int iElements1, int iElements2, T Value )
    {
       for ( int I = 0; I < iElements1; I++ )
       {
          for ( int C = 0; C < iElements2; C++ )
          {
             Array[I][C] = Value;
          }
       }
    }
    
    int main()
    {
       bool Visited[10][10];
       Initialize2D<bool, 10>( Visited, 10, 10, false );
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    That worked fine, thank you.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

Popular pages Recent additions subscribe to a feed