Passing string array as pointer
-
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? -
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?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
andl.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
-
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?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)
-
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
andl.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
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?
-
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)
-
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?
__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
-
__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
-
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.
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)
-
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
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
-
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?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