help.

I do everything in C++ with STL but I have to utilitize legacy C functions within my member functions (methods).

The legacy C function signature is...

extern int UF_DRF_create_label(
int num_lines_text,
char text_string[ ][132+1] ,
double origin_3d[3],
UF_DRF_leader_attach_type_t leader_attach_type,
UF_DRF_object_p_t object,
double model_pos_3d[ 3 ],
tag_t * label_tag);

My issue is converting my std::vector<std::string> into the char text_string[][132+1].

Please, some sample code ideals!!!

Regards.

I assume num_lines_text is the valid size of the 2d array. If so

vector<string> theList;
for(int i = 0; i < num_lines_text; i++)
    theList.push_back(text_string[i]);

Thank you for the reply,

but wrong way, I believe you're showing pushing an array of character strings into a vector<string>.

for example only

std:vector<std::string> text;
text.push_back("Line One of Text");
text.push_back("Line Two of Text");
text.push_back("Line Three of Text");

// I need to convert this to...

int n_lines_text = static_cast<int>(text.size());

char *ctext[133];

// ... so to use in ...

int rv = UF_DRF_create_label(n_lines_text, ctext, etc...);

Regards.

Thank you for the reply,

but wrong way, I believe you're showing pushing an array of character strings into a vector<string>.

Regards.

Not quite. It would be valid if your array consisted of const char* values because strings accept const char* (and also chars) as a constructor argument via the ADT implementation in C++. Since the constructor of the string class isn't marked explicit, using const char* arguments where a string is expected will cause the compiler to generate a string object with the given const char* argument as a parameter for the constructor.

So during a push_back call using a const char* for a vector<string>, you will generate string objects to be stored inside the vector<string>

string

vector

Well, tried it and the complied spit back it can not convert from std::vector to char [][133], as I would expect would happen when a function is expecting a char text[][133] and I try passing a std::vector<std::string>.

Its gotten to be along the lines of char *text[133] or char text[12][133], the 12 is just an arbitrary value, this works as long as at runtime its not required to have 13 lines of texts, buff over flow. I prefer the char *text[133], the length by th edefinition of the function is limited each line of text to 133 char long, but I can have as many lines of text at runtime.

Thanks again.

>>but wrong way
Oh, now I see what you want to do. To my knowledge you can't do it. char *ctext[133]; is an array of 133 pointers but you want an array of n_lines_text number of pointers. It might be possible with a compiler that is C99 compliant, but most compilers aren't. You need char *ctext[n_lines_text]; or possibly even char ctext[n_lines_text][133];

>>but wrong way
Oh, now I see what you want to do. To my knowledge you can't do it. char *ctext[133]; is an array of 133 pointers but you want an array of n_lines_text number of pointers. It might be possible with a compiler that is C99 compliant, but most compilers aren't. You need char *ctext[n_lines_text]; or possibly even char ctext[n_lines_text][133];

THANK YOU, now we're on the same page.

the char ctext[n_line_text][133] is not possible, n_line_text is not const, this varies depending the number of lines in the std::vector<std:string> .

any ideals on this one char *ctext[133] ? this would worked, actually i believe it may be this form char (*ctext)[133] , no thinking in type, this would be array of 133 char pointers.

right now, I'm using the arbitrary value of 12 lines of text, this is fine for now. but I really want a dynamic method so to have any number of lines of text.

Regards.

>>the char ctext[n_line_text][133] is not possible, n_line_text is not const
It is possible with a c99 compliant compiler. But not very many compilers have implemented that yet.

just copy all the string data into a temporary buffer and pass it to the function. e.g.

main(){
   vector<string> st;
   
   st.push_back("test11g 1");
   st.push_back("tes22ng 2");
   st.push_back("test3ng 3");
   st.push_back("test44g 4");
   st.push_back("testi55 5");
   st.push_back("testin666");
   st.push_back("testi77 7");
   
   char *st2 = new char[st.size() * 133];
   for(int i = 0; i < st.size(); i++)
      strcpy(st2 + i*133, st[i].c_str());
      
   fn((char(*)[133])st2, st.size());
   
   delete[] st2;
   
   }

Or without casts and tricky subscript maths

char (*st2)[133] = new char[st.size()][133];
for(int i = 0; i < st.size(); i++)
  strcpy(st2[i], st[i].c_str());
fn(st2, st.size());
commented: great suggestion :) +36

Or without casts and tricky subscript maths

char (*st2)[133] = new char[st.size()][133];
for(int i = 0; i < st.size(); i++)
  strcpy(st2[i], st[i].c_str());
fn(st2, st.size());

EXCELLENT!
This works perfectly, tested in five different scenarios!

Thank you, now I can have a dynamic list of strings and not waste allocating unneed memory space.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.