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. Problem with qsort() on CArray(ptr *, ptr *)

Problem with qsort() on CArray(ptr *, ptr *)

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
6 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.
  • J Offline
    J Offline
    john john mackey
    wrote on last edited by
    #1

    can someone help me? I have a variable declared as CArray<CmyData *, CmyData*> myDataArray; then I try the following:

    CmyData** pData = (CmyData **) myDataArray.GetData();
    qsort(pData, numItems, sizeof(CmyData *), Compare);

    Something is whacky with how Compare() function receives the information from pData. Pointers go off to neverland?? Thank you. RESOLVED: My Compare function had to be adjust to handle array of pointers:

    myClass::Compare(const void* pEle1, const void* pEle2)
    {
    CmyData* pData1 = *(CmyData **) pEle1;
    CmyData* pData2 = *(CmyData **) pEle2;
    // do the comparison, now that the defrenced pointer good

    }

    Richard Andrew x64R C 3 Replies Last reply
    0
    • J john john mackey

      can someone help me? I have a variable declared as CArray<CmyData *, CmyData*> myDataArray; then I try the following:

      CmyData** pData = (CmyData **) myDataArray.GetData();
      qsort(pData, numItems, sizeof(CmyData *), Compare);

      Something is whacky with how Compare() function receives the information from pData. Pointers go off to neverland?? Thank you. RESOLVED: My Compare function had to be adjust to handle array of pointers:

      myClass::Compare(const void* pEle1, const void* pEle2)
      {
      CmyData* pData1 = *(CmyData **) pEle1;
      CmyData* pData2 = *(CmyData **) pEle2;
      // do the comparison, now that the defrenced pointer good

      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Perhaps you should give more detail, like exactly what goes wrong, error message, etc..

      The difficult we do right away... ...the impossible takes slightly longer.

      J 1 Reply Last reply
      0
      • J john john mackey

        can someone help me? I have a variable declared as CArray<CmyData *, CmyData*> myDataArray; then I try the following:

        CmyData** pData = (CmyData **) myDataArray.GetData();
        qsort(pData, numItems, sizeof(CmyData *), Compare);

        Something is whacky with how Compare() function receives the information from pData. Pointers go off to neverland?? Thank you. RESOLVED: My Compare function had to be adjust to handle array of pointers:

        myClass::Compare(const void* pEle1, const void* pEle2)
        {
        CmyData* pData1 = *(CmyData **) pEle1;
        CmyData* pData2 = *(CmyData **) pEle2;
        // do the comparison, now that the defrenced pointer good

        }

        C Offline
        C Offline
        Chuck OToole
        wrote on last edited by
        #3

        Excellent article here[^], the examples should help.

        1 Reply Last reply
        0
        • J john john mackey

          can someone help me? I have a variable declared as CArray<CmyData *, CmyData*> myDataArray; then I try the following:

          CmyData** pData = (CmyData **) myDataArray.GetData();
          qsort(pData, numItems, sizeof(CmyData *), Compare);

          Something is whacky with how Compare() function receives the information from pData. Pointers go off to neverland?? Thank you. RESOLVED: My Compare function had to be adjust to handle array of pointers:

          myClass::Compare(const void* pEle1, const void* pEle2)
          {
          CmyData* pData1 = *(CmyData **) pEle1;
          CmyData* pData2 = *(CmyData **) pEle2;
          // do the comparison, now that the defrenced pointer good

          }

          C Offline
          C Offline
          Chuck OToole
          wrote on last edited by
          #4

          And another relevant article, with examples here[^] Gee, Google is a great tool, you should use it

          J 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Perhaps you should give more detail, like exactly what goes wrong, error message, etc..

            The difficult we do right away... ...the impossible takes slightly longer.

            J Offline
            J Offline
            john john mackey
            wrote on last edited by
            #5

            Sorry, i think the brackets watered down my description. I have declared a CArray as:

            CArray<CmyData *, CmyData *> myDataArray;

            I fill myDataArray correctly using CArray::SetAtGrow(). This is done correctly because I test by iterating through the CArray after i fill it and check all the items in the CmyData object. Then i attempt:

            CmyData** pData = (CmyData **) myDataArray.GetData();
            // verified both myDataArray and pData can report back my data - iterated thru

            qsort(pData, numItems, sizeof(CmyData *), Compare);

            Then when i look in my Compare() function, the 2 parameters have invalid values. I don't know if there is a problem passing pData like i do OR if my Compare() function is not resolving the 2 parameters correctly. (PS, thanks for the other posts on articles to read..)

            1 Reply Last reply
            0
            • C Chuck OToole

              And another relevant article, with examples here[^] Gee, Google is a great tool, you should use it

              J Offline
              J Offline
              john john mackey
              wrote on last edited by
              #6

              Thanks! the first link you provided as a reference looks exactly like the setup i am using. I also updated my post under one of the first replys - the < and > brackets may have corrupted my original question. JJM

              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