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. Pointers to arrays of pointers.

Pointers to arrays of pointers.

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
4 Posts 3 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.
  • O Offline
    O Offline
    oRion
    wrote on last edited by
    #1

    I would like to access a 2D array using pointers. I understand that I could use function such as pointers to array of pointers to achieve that. How do I actually do that? and how do I allocate memory to it using new and delete function? X| -Danny

    C 1 Reply Last reply
    0
    • O oRion

      I would like to access a 2D array using pointers. I understand that I could use function such as pointers to array of pointers to achieve that. How do I actually do that? and how do I allocate memory to it using new and delete function? X| -Danny

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

      int *ptrArray[10];
      for(int i=0;i<10;i++)
      {
      ptrArray[i]= new int[10];
      }
      ...
      ... whatever
      ...
      for(i=0;i<10;i++)
      {
      delete [] ptrArray[i];}

      -c


      Cheap oil. It's worth it!

      ISEffects - effects for images

      O 1 Reply Last reply
      0
      • C Chris Losinger

        int *ptrArray[10];
        for(int i=0;i<10;i++)
        {
        ptrArray[i]= new int[10];
        }
        ...
        ... whatever
        ...
        for(i=0;i<10;i++)
        {
        delete [] ptrArray[i];}

        -c


        Cheap oil. It's worth it!

        ISEffects - effects for images

        O Offline
        O Offline
        oRion
        wrote on last edited by
        #3

        Thanks for the reply. I udnerstand that is for 1D pointers? I have come across the code for 2D array pointers using int **a; // a is a 2D array of size Col 10 x Row 5 int row = 5; int col = 10; a = new int*[row]; for (int i=0,i

        J 1 Reply Last reply
        0
        • O oRion

          Thanks for the reply. I udnerstand that is for 1D pointers? I have come across the code for 2D array pointers using int **a; // a is a 2D array of size Col 10 x Row 5 int row = 5; int col = 10; a = new int*[row]; for (int i=0,i

          J Offline
          J Offline
          jan larsen
          wrote on last edited by
          #4

          oRion wrote: Thanks for the reply. I udnerstand that is for 1D pointers?

          the compiler says: Ok, i take the memory offset of the_array and add the the size of an element times x and then i dereference (or whatever that is called) it. So, when you declare the_array to be int ** the_array, then you say that you want a pointer to an object that can reside anywhere to a pointer to an object that can reside anywhere. When you declare the_array to be int * the_array, then you say that you want a pointer to an object that resides on the stack that consists of pointer(s) to object(s) that can reside anywhere. If an array is statically declared, then you can use the sizeof() operator to get the number of items, otherwise you have to remember the size yourself.
          oRion wrote: how do I actually delete it? I have tried to use delete[] a; If the 2D array was declared as you stated, then no, this is not sufficient. You have to traverse the rows and delete[] each column array first, then delete[] the rows array. If on the other hand the array was declared as int * the_array[10], then you only had to delete[] the column arrays as the rows array is declared on the stack. "It could have been worse, it could have been ME!"

          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