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. Passing string array as pointer

Passing string array as pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
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
    __DanC__
    wrote on last edited by
    #1

    I have a std::list of strings which I need to pass to another function as an LPARAM, so I believe I need to create an array of char* and copy each string in but how do I create a char* array of the correct size (it says it requires a constant)? And how do I know the size of the array once they are passed to the second function? Or is there a better way to do this?

    D M S 3 Replies Last reply
    0
    • _ __DanC__

      I have a std::list of strings which I need to pass to another function as an LPARAM, so I believe I need to create an array of char* and copy each string in but how do I create a char* array of the correct size (it says it requires a constant)? And how do I know the size of the array once they are passed to the second function? Or is there a better way to do this?

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

      Something like (which might be overkill):

      char **arr = new char*[l.size()];
      int y = 0;
      for (list<string>::iterator x = l.begin(); x != l.end(); x++)
      {
      string str = *x;
      arr[y] = new char[str.length() + 1];
      strcpy(arr[y], str.c_str());
      y++;
      }

      Now you can pass arr and l.size() as the arguments to your function. You could also do something like:

      void foo( LPARAM lParam )
      {
      list<string> *l = (list<string> *) lParam;
      }
      ...
      list<string> l;
      foo((LPARAM) &l);

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      _ 1 Reply Last reply
      0
      • _ __DanC__

        I have a std::list of strings which I need to pass to another function as an LPARAM, so I believe I need to create an array of char* and copy each string in but how do I create a char* array of the correct size (it says it requires a constant)? And how do I know the size of the array once they are passed to the second function? Or is there a better way to do this?

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Why don't you just pass the address of the std::list as the LPARAM? If you need a copy at the other end, copy the std::list and use the address of the copy.:~

        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

        _ 1 Reply Last reply
        0
        • D David Crow

          Something like (which might be overkill):

          char **arr = new char*[l.size()];
          int y = 0;
          for (list<string>::iterator x = l.begin(); x != l.end(); x++)
          {
          string str = *x;
          arr[y] = new char[str.length() + 1];
          strcpy(arr[y], str.c_str());
          y++;
          }

          Now you can pass arr and l.size() as the arguments to your function. You could also do something like:

          void foo( LPARAM lParam )
          {
          list<string> *l = (list<string> *) lParam;
          }
          ...
          list<string> l;
          foo((LPARAM) &l);

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

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

          Thanks! That is very similar to what I had been working towards (apart from the fact it works!) So now, in my callback I am using:

          char **arr = (char**)lParam;
          int count = sizeof(arr) / sizeof(*arr);

          But this gives me 1 when it should be 2, what is the correct way to determine the size of the array?

          D 1 Reply Last reply
          0
          • M Matthew Faithfull

            Why don't you just pass the address of the std::list as the LPARAM? If you need a copy at the other end, copy the std::list and use the address of the copy.:~

            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

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

            I didn't realise that was possible. How would I copy the list? I tried CopyMemory before I started down this route with no luck so decided the pointer array was probably the way to go.

            M 1 Reply Last reply
            0
            • _ __DanC__

              Thanks! That is very similar to what I had been working towards (apart from the fact it works!) So now, in my callback I am using:

              char **arr = (char**)lParam;
              int count = sizeof(arr) / sizeof(*arr);

              But this gives me 1 when it should be 2, what is the correct way to determine the size of the array?

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

              __DanC__ wrote:

              But this gives me 1 when it should be 2...

              Right, because a pointer is four bytes in size.

              __DanC__ wrote:

              ...what is the correct way to determine the size of the array?

              As a second argument to your function.

              "Love people and use things, not love things and use people." - Unknown

              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

              _ 1 Reply Last reply
              0
              • D David Crow

                __DanC__ wrote:

                But this gives me 1 when it should be 2...

                Right, because a pointer is four bytes in size.

                __DanC__ wrote:

                ...what is the correct way to determine the size of the array?

                As a second argument to your function.

                "Love people and use things, not love things and use people." - Unknown

                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                _ Offline
                _ Offline
                __DanC__
                wrote on last edited by
                #7

                I do not have the option of a second argument to the function as it is a callback. All I can pass is the pointer as an LPARAM

                D 1 Reply Last reply
                0
                • _ __DanC__

                  I didn't realise that was possible. How would I copy the list? I tried CopyMemory before I started down this route with no luck so decided the pointer array was probably the way to go.

                  M Offline
                  M Offline
                  Matthew Faithfull
                  wrote on last edited by
                  #8

                  It's a while since I've used std::list so you're probably best looking at the docs but I guess you could simply create a second std::list, then iterate through the first one, use CopyMemory or whatever for each item and then append it to the second list. I'm not sure if you can simply assign one list to another or pass the first list to a constructor of the second. I suspect it depends on what your list contains, simple POV type, complex types, pointers, etc. Reminds me I need to look into the std containers once again. I've never really got on with them or used them on a regular basis.

                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                  1 Reply Last reply
                  0
                  • _ __DanC__

                    I do not have the option of a second argument to the function as it is a callback. All I can pass is the pointer as an LPARAM

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

                    So put the array and the count in a structure and pass the address of that structure.

                    "Love people and use things, not love things and use people." - Unknown

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    1 Reply Last reply
                    0
                    • _ __DanC__

                      I have a std::list of strings which I need to pass to another function as an LPARAM, so I believe I need to create an array of char* and copy each string in but how do I create a char* array of the correct size (it says it requires a constant)? And how do I know the size of the array once they are passed to the second function? Or is there a better way to do this?

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #10

                      Do you really need to copy the list? Why not something like this:

                      void IWantAList(LPARAM lp)
                      {
                      using namespace std;
                       
                      list<string> *pList = reinterpret_cast<list<string>*>(lp);
                       
                      // Use pList...
                      }
                       
                      void SendList()
                      {
                      using namespace std;
                       
                      list<string> list;
                       
                      // ... //
                       
                      IWantAList(reinterpret_cast<LPARAM>(&list));
                      }

                      Steve

                      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