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. declaring 2 D array

declaring 2 D array

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
4 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help

    A B 2 Replies Last reply
    0
    • A Anonymous

      I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help

      A Offline
      A Offline
      AlexO
      wrote on last edited by
      #2

      only last dimension can be undefined try __int8 stepArray[2][]={{1,2,3,4},{5,6,7,8}}; better yet use std::vector

      D 1 Reply Last reply
      0
      • A Anonymous

        I am getting confused in declaring a 2d array I need a n*4 matrix (n rows with 4 cols in each row) I wanted to do something like __int8 stepArray[][]={{1,2,3,4},{5,6,7,8}}; corresponds to 1 2 3 4 5 6 7 8 where n is implicitly 2 I am getting an error . Please help

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        you can use pointers and memmory allocation to do the trick! try this, it should work, I had an assignment on this, a while ago... Let's see if I can dig up some code :)

        __int8 *stepArray[4] = NULL; // declaration

        // n should be the number of rows you want, not the highest index!!!
        // the best way to insert elements is through a function or to embed this
        // array in a class
        stepArray = (__int8 **) malloc(n * sizeof(__int8*)); // the number of rows you want
        // allocate for each row 4 columns
        while(n-- > 0)
        stepArray[n] = (__int8*) malloc(sizeof(__int8));

        after this, you can use it as a normal 2d matrix (stepArray[n][m] =...) It is far more easy to use a vector of some sort, but it's more fun to do it yourself (at least, I experience it that way :)) hope this helps :) ps. I don't want te scare you, but beware of memory leaks, because they sneak in very easy...

        A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

        1 Reply Last reply
        0
        • A AlexO

          only last dimension can be undefined try __int8 stepArray[2][]={{1,2,3,4},{5,6,7,8}}; better yet use std::vector

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

          Actually, it's the opposite. The first dimension can be undefined, as in: __int8 stepArray[][4]={{1,2,3,4},{5,6,7,8}};

          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