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. Copy a pointer to an element of an array of pointers

Copy a pointer to an element of an array of pointers

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
5 Posts 5 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.
  • M Offline
    M Offline
    Majid Shahabfar
    wrote on last edited by
    #1

    How can I copy a pointer to an item of an array pointer such as below? ClassA *ptr = new ClassA[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; // Error &(ptr[1]) = ptr2; // Error What should I do?

    H D J P 4 Replies Last reply
    0
    • M Majid Shahabfar

      How can I copy a pointer to an item of an array pointer such as below? ClassA *ptr = new ClassA[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; // Error &(ptr[1]) = ptr2; // Error What should I do?

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #2

      The array pointer declaration could be:

      ClassA **ptr = new ClassA*[5];

      Then use ptr[i] = ClassA_Ptr_. rechi

      1 Reply Last reply
      0
      • M Majid Shahabfar

        How can I copy a pointer to an item of an array pointer such as below? ClassA *ptr = new ClassA[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; // Error &(ptr[1]) = ptr2; // Error What should I do?

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

        ptr[0] is an actual object, not a pointer to an object. Therefore, it needs to be assigned the value of ptr1, not the address of ptr1. ptr[0] = *ptr1;

        1 Reply Last reply
        0
        • M Majid Shahabfar

          How can I copy a pointer to an item of an array pointer such as below? ClassA *ptr = new ClassA[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; // Error &(ptr[1]) = ptr2; // Error What should I do?

          J Offline
          J Offline
          jhaga
          wrote on last edited by
          #4

          If you want an array of pointers then do like this ClassA *ptr[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; and now ptr[0]=ptr1; jhaga

          1 Reply Last reply
          0
          • M Majid Shahabfar

            How can I copy a pointer to an item of an array pointer such as below? ClassA *ptr = new ClassA[5]; ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; // Error &(ptr[1]) = ptr2; // Error What should I do?

            P Offline
            P Offline
            Peter Mares
            wrote on last edited by
            #5

            the line ClassA *ptr = new ClassA[5]; defines an array of 5 ClassA objects, not an array of 5 ClassA pointers. If you want an array of 5 ClassA pointers, your declaration will have to be: ClassA **ptr = new ClassA*[5]; and then you will be able to execute the following code (or similar): ClassA *ptr1 = somePointerOFClassA; ClassA *ptr2 = somePointerOFClassA; ptr[0] = ptr1; ptr[1] = ptr2; Now, coming back to your previously mentioned code block.Unfortunately, once you have defined an array as you have : ClassA *ptr = new ClassA[5]; You cannot actually change the address of an element within that array, ie, you cannot do this: &ptr[0] = ptr1


            Hope this helps some. Cheers, Peter ------------------------------------------- 99 little bugs in the code, 99 little bugs, Fix 1 bug, recompile.... 101 little bugs in the code...

            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