views:

149

answers:

3

I am doing some serial port communcation to a computer controlled pump and the createfile function I used to communicate requires the com port name to be parsed as a wchar_t pointer.

I am also using QT to create a form and aquire the com port name as a QString.

This QString is converted to a char array and pointed to as follows:

   char* Dialog::GetPumpSerialPortNumber(){
   QString mystring;
   mystring = ui->comboBox_2->currentText();
   char * mychar;
   mychar = mystring.toLatin1().data();
   return mychar;

I now need to set my port number which is stored as a wchar_t* in my pump object. I do this by calling the following function:

   void pump::setPortNumber(wchar_t* portNumber){
       this->portNumber = portNumber;
   }

Thus how do I change my char* (mychar) into a wchar_t* (portNumber)?

Thanks.

A: 

QString::toWCharArray ( wchar_t * array ) ?

teukkam
I'm clearly doing something wrong my code: wchar_t* Dialog::GetPumpSerialPortNumber(){ QString mystring; mystring = ui->comboBox_2->currentText(); wchar_t * mycharPointer; wchar_t mychar[mystring.size()]; mychar[mystring.size()-1] = '\n'; mystring.toWCharArray(mychar); mycharPointer = mychar; cout << *mycharPointer; return mycharPointer;}seems to return an address rather than the char array to "COM6"
Zac
A: 

You can use the toWCharArray function of QString to have your wchar_t* value and return a wchar_t* from your GetPumpSerialPortNumber function.

Patrice Bernassola
A: 

I'm clearly doing something wrong my code:

wchar_t* Dialog::GetPumpSerialPortNumber(){
   QString mystring;
   mystring = ui->comboBox_2->currentText();       // get mystring from form
   wchar_t * mycharPointer;                        // create pointer to return
   wchar_t mychar[mystring.size()];                // allocate space for wchar array
   mychar[mystring.size()-1] = '\n';               // null terminate wchar array
   mystring.toWCharArray(mychar);                  // put mystring in wchar array
   mycharPointer = mychar;                         // point at wchar array
   return mycharPointer;                           // return wchar array string
}

seems to return an address rather than the char array to "COM6"

Zac
That's what arrays in C++ are, it's simply a pointer to the first slot in the array. Don't forget to add the dereferencing operator `*` when using the value returned from the function.
teukkam
PS. You should not add addition to the original question as an answer. You may edit the original question freely.
teukkam
You are also initializing the array with a char literal. That might be a problem. You should leave in uninitialized since it will be replaced with the return argument from toWCharArray anyway. In C(++), single quotes mean char literal, not null-terminated string literal which is marked with double quotes.
teukkam
You still have a dangling pointer problem in this solution as well. You return a pointer to the beginning of the mychar array, which goes out of scope at the end of the function (and becomes free memory).
Caleb Huitt - cjhuitt
How should I solve this dangling pointer issue? Is there a new command or something like that to allocate the char array on the heap? If so and the function is called many times when will the new array get freed?Thanks
Zac