Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multi arrays

Multi arrays

Scheduled Pinned Locked Moved C / C++ / MFC
cssdata-structureshelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bitsNbites
    wrote on last edited by
    #1

    What I want to do is take an input from the user to set up a grid. This grid is a multi- dimensional array. I can do this with a simple array (1-D) and create the object as new. But when I try this with a multi- dimensional I get the error "constant expression expected." If only I had more time!

    D 1 Reply Last reply
    0
    • B bitsNbites

      What I want to do is take an input from the user to set up a grid. This grid is a multi- dimensional array. I can do this with a simple array (1-D) and create the object as new. But when I try this with a multi- dimensional I get the error "constant expression expected." If only I had more time!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      bitsNbites wrote: But when I try this with a multi- dimensional I get the error "constant expression expected." Without showing the code in error, it's impossible to offer any suggestion(s).


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      B 1 Reply Last reply
      0
      • D David Crow

        bitsNbites wrote: But when I try this with a multi- dimensional I get the error "constant expression expected." Without showing the code in error, it's impossible to offer any suggestion(s).


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        B Offline
        B Offline
        bitsNbites
        wrote on last edited by
        #3

        The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!

        D P 2 Replies Last reply
        0
        • B bitsNbites

          The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          See here.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          1 Reply Last reply
          0
          • B bitsNbites

            The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!

            P Offline
            P Offline
            Priyank Bolia
            wrote on last edited by
            #5

            int _tmain(int argc, _TCHAR* argv[]) { int** sizeArray; int row=22, col=20; sizeArray = new int*[row]; for(int i=0; i<row; i++) sizeArray[i] = new int[col]; for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { sizeArray[i][j] = i*j; } for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { printf("sizeArray[%d][%d] = %d\n",i, j, sizeArray[i][j]); } return 0; }
            http://www.priyank.in/

            B 1 Reply Last reply
            0
            • P Priyank Bolia

              int _tmain(int argc, _TCHAR* argv[]) { int** sizeArray; int row=22, col=20; sizeArray = new int*[row]; for(int i=0; i<row; i++) sizeArray[i] = new int[col]; for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { sizeArray[i][j] = i*j; } for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { printf("sizeArray[%d][%d] = %d\n",i, j, sizeArray[i][j]); } return 0; }
              http://www.priyank.in/

              B Offline
              B Offline
              bitsNbites
              wrote on last edited by
              #6

              Great that helps a lot. I now am using: int** q = new int*[rows]; for(int i = 0; i < rows; i++) q[i] = new int [col]; This works. MSDN states "Multidimensional arrays are not equivalent to arrays of pointers." It seems that here this code is creating just that. I suspect this is why I don't see the elements in the Autos window. I will play around with this for a while and will post new questions later. Thanks for the help. If only I had more time!

              P 1 Reply Last reply
              0
              • B bitsNbites

                Great that helps a lot. I now am using: int** q = new int*[rows]; for(int i = 0; i < rows; i++) q[i] = new int [col]; This works. MSDN states "Multidimensional arrays are not equivalent to arrays of pointers." It seems that here this code is creating just that. I suspect this is why I don't see the elements in the Autos window. I will play around with this for a while and will post new questions later. Thanks for the help. If only I had more time!

                P Offline
                P Offline
                Priyank Bolia
                wrote on last edited by
                #7

                You can always create multi dimensional arrays by arrays of pointers pointing to another array. The difference I think is basically in memory locations, creating arrays by this method dynamically may not have continous memory locations, like that by creating arrays like int a[5][5]; http://www.priyank.in/

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups