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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. two dimensional array initialization

two dimensional array initialization

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiodata-structureshelptutorial
17 Posts 5 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.
  • T toxcct

    the best ? std::vector<std::vector<std::string> >


    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #4

    Thanks toxcct, I need to use C other than C++. What do you think is the best solution in C? regards, George

    C 1 Reply Last reply
    0
    • R Rage

      George_George wrote:

      const char foo[][] = {"hello", "world"}; // compile error const char goo[][64] = {"hello", "world"}; // compile correc

      const char *foo[]= {"hello","world"};

      Don't follow any man spiritually, don't do anything that will get you in sh*t if god is real - Bradml[^]

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #5

      Cool! regards, George

      M 1 Reply Last reply
      0
      • G George_George

        Thanks toxcct, I need to use C other than C++. What do you think is the best solution in C? regards, George

        C Offline
        C Offline
        codeII
        wrote on last edited by
        #6

        Dynamic you can do it something like this: int nNmbOfStrings = 2; int nI; LPTSTR* pArr = new LPTSTR[ nNmbOfStrings ]; pArr[ 0 ] = new TCHAR[ _tcslen( _T("hello") ) + 1 ]; pArr[ 1 ] = new TCHAR[ _tcslen( _T("world") ) + 1 ]; _tcscpy( pArr[ 0 ], _T("hello") ); _tcscpy( pArr[ 1 ], _T("world") ); //deletion: for ( nI = 0; nI < nNmbOfStrings; nI++ ) { delete [] pArr[ nI ]; pArr[ nI ] = NULL; } delete [] pArr; pArr = NULL;

        G 1 Reply Last reply
        0
        • C codeII

          Dynamic you can do it something like this: int nNmbOfStrings = 2; int nI; LPTSTR* pArr = new LPTSTR[ nNmbOfStrings ]; pArr[ 0 ] = new TCHAR[ _tcslen( _T("hello") ) + 1 ]; pArr[ 1 ] = new TCHAR[ _tcslen( _T("world") ) + 1 ]; _tcscpy( pArr[ 0 ], _T("hello") ); _tcscpy( pArr[ 1 ], _T("world") ); //deletion: for ( nI = 0; nI < nNmbOfStrings; nI++ ) { delete [] pArr[ nI ]; pArr[ nI ] = NULL; } delete [] pArr; pArr = NULL;

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #7

          Thanks koos, It works but a little complex. regards, George

          C 1 Reply Last reply
          0
          • G George_George

            Thanks koos, It works but a little complex. regards, George

            C Offline
            C Offline
            codeII
            wrote on last edited by
            #8

            Yep, It only makes sence when you dynamically want assign/reassign an array. //f.i. reassign string 2 (before deletion!): delete [] pArr[ 1 ]; pArr[ 1 ] = new TCHAR[ _tcslen( _T("Every body") ) + 1 ]; _tcscpy( pArr[ 1 ], _T("Every body") ); Regards, Koos

            G 1 Reply Last reply
            0
            • G George_George

              Hello everyone, I find to initialize two dimentional array in Visual Studio, I have to specify the number of elements. For example, const char foo[][] = {"hello", "world"}; // compile error const char goo[][64] = {"hello", "world"}; // compile correct So, the best solution is to specify the number of elements of the 2nd dimension (inner dimension)? thanks in advance, George

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #9

              If you want to stick with fixed-sized array bounds, you have to specify all bounds except the first. That's why [][] is an error but [][64] is OK.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              G 1 Reply Last reply
              0
              • G George_George

                Cool! regards, George

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #10

                Keep in mind that George's solution is not the same as your original code. Most notably, you shouldn't write to any of the memory occupied by the strings, whereas with regular arrays, it's always safe to write to that memory.

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                G 1 Reply Last reply
                0
                • C codeII

                  Yep, It only makes sence when you dynamically want assign/reassign an array. //f.i. reassign string 2 (before deletion!): delete [] pArr[ 1 ]; pArr[ 1 ] = new TCHAR[ _tcslen( _T("Every body") ) + 1 ]; _tcscpy( pArr[ 1 ], _T("Every body") ); Regards, Koos

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #11

                  Thanks koos, I am aware that your method has advantage. regards, George

                  1 Reply Last reply
                  0
                  • M Michael Dunn

                    If you want to stick with fixed-sized array bounds, you have to specify all bounds except the first. That's why [][] is an error but [][64] is OK.

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #12

                    Thanks Mike, Could you explain from compiler internal point of view why I need to provide the innermost size, why I can not make it empty (e.g. [][])? I think it must break some rules or make compiler confused to make proper decision to generate object code, but I can not find why. regards, George

                    M 1 Reply Last reply
                    0
                    • M Michael Dunn

                      Keep in mind that George's solution is not the same as your original code. Most notably, you shouldn't write to any of the memory occupied by the strings, whereas with regular arrays, it's always safe to write to that memory.

                      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #13

                      Thanks Mike, 1. Why his solution is not the same as mine? 2. How memory corruption will occur? Could you let me know an example please? I am using the array as constant string, so I can not imagine a case which will cause memory corruption. regards, George

                      M 1 Reply Last reply
                      0
                      • G George_George

                        Thanks Mike, 1. Why his solution is not the same as mine? 2. How memory corruption will occur? Could you let me know an example please? I am using the array as constant string, so I can not imagine a case which will cause memory corruption. regards, George

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #14

                        char s1[] = "foo";
                        char* s2 = "bar";
                        strcpy ( s1, "aaa" ); // ok
                        strcpy ( s2, "bbb" ); // not ok

                        The memory pointed to by s2 should be treated as read-only.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                        G 1 Reply Last reply
                        0
                        • G George_George

                          Thanks Mike, Could you explain from compiler internal point of view why I need to provide the innermost size, why I can not make it empty (e.g. [][])? I think it must break some rules or make compiler confused to make proper decision to generate object code, but I can not find why. regards, George

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #15

                          char s1[4] = "cat";
                          char s2[] = "dog";

                          In both of those cases, the array size is 4. The compiler can deduce the size of s2 from the initializer string, which is 4 chars long.

                          char* s3[2] = { "hello", "bob" };
                          char* s4[] = { "code", "project" };

                          Here, the arrays contain char*. Again, the compiler can deduce the size of s4 because the initializer has two char*s. Now make it a 2-D array (I'll switch to ints since the initializer syntax is more precise)

                          int i5[5][2] = { {1,2,3,4,5}, {6,7,8,9,10} };

                          i5 is an array of two int[5] arrays. Now if I did this:

                          int i6[5][] = { {1,2,3,4,5}, {6,7,8,9,10}, {0,2,4,6,8} };

                          Since I used three int[5] arrays in the initializer, the compiler deduces that i6 is int[5]**[3]**. (BTW I just realized that my earlier post was wrong, it's the last dimension that you can leave out, not the first.)

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                          G 1 Reply Last reply
                          0
                          • M Michael Dunn

                            char s1[] = "foo";
                            char* s2 = "bar";
                            strcpy ( s1, "aaa" ); // ok
                            strcpy ( s2, "bbb" ); // not ok

                            The memory pointed to by s2 should be treated as read-only.

                            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #16

                            Good point! Thanks Mike! I have not noticed this point before. regards, George

                            1 Reply Last reply
                            0
                            • M Michael Dunn

                              char s1[4] = "cat";
                              char s2[] = "dog";

                              In both of those cases, the array size is 4. The compiler can deduce the size of s2 from the initializer string, which is 4 chars long.

                              char* s3[2] = { "hello", "bob" };
                              char* s4[] = { "code", "project" };

                              Here, the arrays contain char*. Again, the compiler can deduce the size of s4 because the initializer has two char*s. Now make it a 2-D array (I'll switch to ints since the initializer syntax is more precise)

                              int i5[5][2] = { {1,2,3,4,5}, {6,7,8,9,10} };

                              i5 is an array of two int[5] arrays. Now if I did this:

                              int i6[5][] = { {1,2,3,4,5}, {6,7,8,9,10}, {0,2,4,6,8} };

                              Since I used three int[5] arrays in the initializer, the compiler deduces that i6 is int[5]**[3]**. (BTW I just realized that my earlier post was wrong, it's the last dimension that you can leave out, not the first.)

                              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                              G Offline
                              G Offline
                              George_George
                              wrote on last edited by
                              #17

                              Hi Mike, The code, int i6[5][] = { {1,2,3,4,5}, {6,7,8,9,10}, {0,2,4,6,8} }; will not compile. You should use, int i6[][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {0,2,4,6,8} }; or int i6[3][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {0,2,4,6,8} }; regards, George

                              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