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. 2 dimmensional dynamic array of pointers

2 dimmensional dynamic array of pointers

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
7 Posts 6 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.
  • L Offline
    L Offline
    Liborac_
    wrote on last edited by
    #1

    HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

    C A A R L 5 Replies Last reply
    0
    • L Liborac_

      HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      #include < vector > ... std::vector< CMyType * > my_dynamic_array; Image Toolkits | Image Processing | Cleek

      1 Reply Last reply
      0
      • L Liborac_

        HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        int a = 17; int* (*pi)[32] = new int* [32][32]; pi[31][31] = &a; cout << *pi[31][31] << endl; delete [] pi;

        1 Reply Last reply
        0
        • L Liborac_

          HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

          A Offline
          A Offline
          Alton Williams
          wrote on last edited by
          #4

          Declare as such: int **myArray These ways to allocate them myArray = new[] int [rows][cols]; or myArray = new[] int[rows * cols]; myArray = (int *) malloc(sizeof(int( rows * cols))); int counter myArray = new[] int* [rows] for (counter = 0; counter < rows; counter++) { myArray[counter] = new[] int [cols]; } don't forget to check if the memory has been allocated (if(myArray) ). This presents you using and dereferencing a NULL pointer. Good luck, Alton

          1 Reply Last reply
          0
          • L Liborac_

            HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

            R Offline
            R Offline
            Rick York
            wrote on last edited by
            #5

            I like to access the data with the double bracket-style syntax, data[x][y], so this is what I do :

            void **Allocate2DArray( int xdim, int ydim, int itemsize )
            {
            int x;
            void **data = (void **)calloc( xdim, sizeof(void *) );
            if( data == NULL )
            return NULL;
            for( x = 0; x < xdim; x++ )

            = calloc( ydim, itemsize );

            }

            This allocates an array of pointers in which each item in the array is a pointer. This is obviously C-style allocation. It can be converted to C++ style using new without too much effort.

            1 Reply Last reply
            0
            • L Liborac_

              HI. Is there a way how to create 2-dimmensional dynamic array of pointers ? Thanks in advance.

              L Offline
              L Offline
              Liborac_
              wrote on last edited by
              #6

              Thanks a lot ;)

              A 1 Reply Last reply
              0
              • L Liborac_

                Thanks a lot ;)

                A Offline
                A Offline
                Axter
                wrote on last edited by
                #7

                Check out following link for safer ways to create a 2 dimensional dynamic array in C++. http://www.tek-tips.com/faqs.cfm?fid=5575[^] Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

                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