|
-
May 17th, 2009, 10:15 AM
#1
Passing an array of char arrays using plain old data types
I need to write a wrapper for some classes that make extensive use of STL string vectors in order to build a DLL that will work with varous CRTs. To do that, I need to find a way to input an array of char arrays using plain old data types (int, char, etc..) to simulate a std::vector<string>. While at first this seems like a relatively simple problem, considering how easy it is to accomplish using int, or float, I have been completely stymied when it comes to building and passing char array arrays.
Using a double array, for example:
Code:
void Func1(double * fvec, int n)
{
cout << "Here's the vector in Func1:" << endl;
for(int i = 0; i < n; i++)
cout << fvec[i] << endl;
}
int main()
{
double * pvec = new double[9];
pvec[0] = 0.123234;
pvec[1] = 0.876765;
pvec[2] = 0.776895;
pvec[3] = 0.345456;
pvec[4] = 0.474474;
pvec[5] = 0.995845;
pvec[6] = 0.765654;
pvec[7] = 0.383338;
pvec[8] = 0.098345;
for(int i = 0; i < 9; i++) cout << pvec[i] << endl;
Func1(pvec, 9); // a vector from simple types
delete [] pvec;
}
The same technique works for int, float, double, but not for char **. One technique that does sort of work is as follows:
Code:
typedef char CharArray[100];
CharArray * pca = new CharArray[9];
strcpy_s(pca[0], 100, "bow");
strcpy_s(pca[1], 100, "wow");
strcpy_s(pca[2], 100, "now");
strcpy_s(pca[3], 100, "cow");
strcpy_s(pca[4], 100, "how");
strcpy_s(pca[5], 100, "sow");
strcpy_s(pca[6], 100, "dow");
strcpy_s(pca[7], 100, "yow");
strcpy_s(pca[8], 100, "mow");
But I havn't figured out how to pass such an char array pointer to a functions by value (Note that one cannot reliably pass even simple data types to a DLL function by reference).
I am simply too embarassed to show all of my failed attempts to accomplish this task. Basically, what needs to be done is:
1 - build a array of character arrays on the heap
2 - pass that array by value to a function that can use it
3 - delete the original array to free the heap
If someone could help me with this, I would be most appreciative.
mpliam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|