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. How to initialize a 2D array correctly? :confused:

How to initialize a 2D array correctly? :confused:

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structuresjsonhelp
5 Posts 4 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.
  • K Offline
    K Offline
    KaKa
    wrote on last edited by
    #1

    Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)

    R D M 3 Replies Last reply
    0
    • K KaKa

      Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      Put a backslash at the end of each line. This will have the compiler ignore the backslash and the newline character treating the next line as part of the previous line.


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote

      1 Reply Last reply
      0
      • K KaKa

        Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)

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

        Can you not do something like:

        int data[32][5] = {{0,0,0,0,0},
        {0,0,0,0,1},
        {0,0,0,1,0},
        {0,0,0,1,1},
        ...
        {0,0,1,0,0},
        {0,0,1,0,1},
        {0,0,1,1,0},
        {1,1,1,1,1}};

        void main( void )
        {
        ...
        }


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • K KaKa

          Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)

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

          You can only initialize an array with { } at the point where it is declared.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

          R 1 Reply Last reply
          0
          • M Michael Dunn

            You can only initialize an array with { } at the point where it is declared.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            My 5 for seeing the obvious where I failed to... :-O


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote

            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