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. 2 dim numbers..

2 dim numbers..

Scheduled Pinned Locked Moved C / C++ / MFC
comtoolsquestion
10 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.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    Again[^]

    ---------------------------- 286? WOWW!:-O

    D R M 3 Replies Last reply
    0
    • _ _8086

      Again[^]

      ---------------------------- 286? WOWW!:-O

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

      Are you simply wanting a 2D array?


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      _ 1 Reply Last reply
      0
      • _ _8086

        Again[^]

        ---------------------------- 286? WOWW!:-O

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        It does not matter. pInt[2][3] is the same as pInt[3][2].

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

        _ 2 Replies Last reply
        0
        • D David Crow

          Are you simply wanting a 2D array?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          _ Offline
          _ Offline
          _8086
          wrote on last edited by
          #4

          Thanks atlast got a reply. No I'm not simply wanting a 2D Array. An array is a group of memory locations that we reserve, Ok in the below statement, I've reserved. 6 Memory locations that has the capability to store 6 integers. And I've got a pointer pointing to that. int* pInt = new int[2*3]; The dimension. It's how we look at the reserved memory location. The memory as of now, looks like : [] [] [] [] [] [] ^ Incrementing the pInt's value would make it go like : [] [] [] [] [] [] -+^ [] [] [] [] [] [] ---++^ This is linear. We can do the same with pInt[0],[1]..etc. Ok this about looking at the memory locations in 1D. Now I want to look at it in 2D. The same memory location. I would want to initialize a 2D pointer to the same memory location. Where I should be able to mention 2 indices. You may take it as 2x3 or 3x2 when you say 2x3, That says, 2 rows and 3 elements in each row. [] [] [] [] [] [] Now tell me how should declare a pointer to the memory location that it would access them as 2D. int* pInt = new int[2*3]; int(** ptr2D)[2][3] = new int*[2]; <--something like this. NOT SURE. If we are gonna look at the same memory locations as 3x2, then it becomes. [] [] [] [] [] [] Here ptr2D[2][0].. would make sense. Because, The number of rows, 0th row, 1th row, 2nd row. Has 3 rows. But in the former case, it would crash because we were looking at it as 2 rows with 3 columns. My question makes sense?

          ---------------------------- 286? WOWW!:-O

          D 1 Reply Last reply
          0
          • R Rage

            It does not matter. pInt[2][3] is the same as pInt[3][2].

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

            _ Offline
            _ Offline
            _8086
            wrote on last edited by
            #5

            Yes, but the question is different.

            ---------------------------- 286? WOWW!:-O

            1 Reply Last reply
            0
            • R Rage

              It does not matter. pInt[2][3] is the same as pInt[3][2].

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

              _ Offline
              _ Offline
              _8086
              wrote on last edited by
              #6

              Rage wrote:

              It does not matter. pInt[2][3] is the same as pInt[3][2].

              Oh, you've said like this? Ok, take up the first one., pInt[2][3]; Try Printing the element pInt[3][0]."Boom" !??!. How can this be eqivalent to pIntp[3][2]. The dimension is different and that's what the question is all about. Looking the same block of memory with different dimensions.

              ---------------------------- 286? WOWW!:-O

              R 1 Reply Last reply
              0
              • _ _8086

                Again[^]

                ---------------------------- 286? WOWW!:-O

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                I don't think you can do what you're trying to do. If you can, I'd like to see the syntax. For a 2 dimension array like this: int myarray[2][3]; myarray is not a pointer.  You can't cast an int pointer to the type of myarray (AFAIK). Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • _ _8086

                  Thanks atlast got a reply. No I'm not simply wanting a 2D Array. An array is a group of memory locations that we reserve, Ok in the below statement, I've reserved. 6 Memory locations that has the capability to store 6 integers. And I've got a pointer pointing to that. int* pInt = new int[2*3]; The dimension. It's how we look at the reserved memory location. The memory as of now, looks like : [] [] [] [] [] [] ^ Incrementing the pInt's value would make it go like : [] [] [] [] [] [] -+^ [] [] [] [] [] [] ---++^ This is linear. We can do the same with pInt[0],[1]..etc. Ok this about looking at the memory locations in 1D. Now I want to look at it in 2D. The same memory location. I would want to initialize a 2D pointer to the same memory location. Where I should be able to mention 2 indices. You may take it as 2x3 or 3x2 when you say 2x3, That says, 2 rows and 3 elements in each row. [] [] [] [] [] [] Now tell me how should declare a pointer to the memory location that it would access them as 2D. int* pInt = new int[2*3]; int(** ptr2D)[2][3] = new int*[2]; <--something like this. NOT SURE. If we are gonna look at the same memory locations as 3x2, then it becomes. [] [] [] [] [] [] Here ptr2D[2][0].. would make sense. Because, The number of rows, 0th row, 1th row, 2nd row. Has 3 rows. But in the former case, it would crash because we were looking at it as 2 rows with 3 columns. My question makes sense?

                  ---------------------------- 286? WOWW!:-O

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

                  _8086 wrote:

                  Now tell me how should declare a pointer to the memory location that it would access them as 2D.

                  int **p = new int*[FirstDimension];
                  for(int i = 0; i < FirstDimension; i++)
                  {
                  p[i] = new int[SecondDimension];
                  for(int j = 0; j < SecondDimension; j++)
                  {
                  p[i][j] = 0;
                  }
                  }


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  _ 1 Reply Last reply
                  0
                  • D David Crow

                    _8086 wrote:

                    Now tell me how should declare a pointer to the memory location that it would access them as 2D.

                    int **p = new int*[FirstDimension];
                    for(int i = 0; i < FirstDimension; i++)
                    {
                    p[i] = new int[SecondDimension];
                    for(int j = 0; j < SecondDimension; j++)
                    {
                    p[i][j] = 0;
                    }
                    }


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    _ Offline
                    _ Offline
                    _8086
                    wrote on last edited by
                    #9

                    Let me see if this satisfies my requirement. Anyway thanks for your reply.

                    ---------------------------- 286? WOWW!:-O

                    1 Reply Last reply
                    0
                    • _ _8086

                      Rage wrote:

                      It does not matter. pInt[2][3] is the same as pInt[3][2].

                      Oh, you've said like this? Ok, take up the first one., pInt[2][3]; Try Printing the element pInt[3][0]."Boom" !??!. How can this be eqivalent to pIntp[3][2]. The dimension is different and that's what the question is all about. Looking the same block of memory with different dimensions.

                      ---------------------------- 286? WOWW!:-O

                      R Offline
                      R Offline
                      Rage
                      wrote on last edited by
                      #10

                      No, you are reserving 6 memory locations. How you read them does not matter.

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

                      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