从函数C++返回值时出错



我有这样的代码,我想在其中从主函数调用char temp()函数。此外,我希望char temp((的返回值是float,因为它是一个温度值,如下所示:-97.21、98.33等

我使用显式和隐式类型转换将buf转换为float,但这也会引发错误。有人能帮忙吗?

#define SERIALTERMINAL      "/dev/ttyUSB0"
#include <errno.h>
#include <fcntl.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %sn", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;         /* 8-bit characters */
tty.c_cflag &= ~PARENB;     /* no parity bit */
tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */
tty.c_lflag |= ICANON | ISIG;  /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);
tty.c_iflag &= ~IGNCR;  /* preserve carriage return */
tty.c_iflag &= ~INPCK;
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY);   /* no SW flowcontrol */
tty.c_oflag &= ~OPOST;
tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %sn", strerror(errno));
return -1;
}
return 0;
}

char temp()
{
char *portname = SERIALTERMINAL;
int fd;
int wlen;
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %sn", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
set_interface_attribs(fd, B115200);
/* simple output */
wlen = write(fd, "Hello!n", 7);
if (wlen != 7) {
printf("Error from write: %d, %dn", wlen, errno);
}
tcdrain(fd);    /* delay for output */
unsigned char buf[83];
/* simple canonical input */
do {
//unsigned char buf[83];
unsigned char *p;
int rdlen;
rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
printf("%s", buf);
} else if (rdlen < 0) {
printf("Error from read: %d: %sn", rdlen, strerror(errno));
} else {  /* rdlen == 0 */
printf("Nothing read. EOF?n");
}
/* repeat read */
} while (1);
return buf;
}
int main() {
unsigned char tem[83];
tem= temp();
printf ("%s", tem);
}

我得到的错误是-->

ser.cpp: In function ‘char temp()’:
ser.cpp:1:29: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
#define SERIALTERMINAL      "/dev/ttyUSB0"
^
ser.cpp:53:22: note: in expansion of macro ‘SERIALTERMINAL’
char *portname = SERIALTERMINAL;
^~~~~~~~~~~~~~
ser.cpp:103:8: error: invalid conversion from ‘unsigned char*’ to ‘char’ [-fpermissive]
return buf;
^~~
ser.cpp:72:19: warning: address of local variable ‘buf’ returned [-Wreturn-local-addr]
unsigned char buf[83];
^~~
ser.cpp: In function ‘int main()’:
ser.cpp:109:11: error: incompatible types in assignment of ‘char’ to ‘unsigned char [83]’
tem= temp();

所以我认为这里有很多问题。

第一个错误很容易解决。只需将char *portname = SERIALTERMINAL;替换为char portname[] = SERIALTERMINAL;,它就会消失。

第二个错误是由于您将temp定义为char temp(),这意味着它返回单个char,而bufchar的数组。将temp的定义更改为char * temp(注意其中的*(应该可以解决这个问题。

第三个错误问题更大。buf使用的存储器基本上是函数堆栈的一部分;有效的";直到函数结束。因此,当您返回它时,基本上就是给调用者一个指向不再有效的内存的指针。为了解决这个问题,您必须动态分配内存(然后确保它也被释放(。通常,最好不要像这里那样将数组作为函数堆栈的一部分,因为它们往往很容易受到溢出攻击。

最后,最后一个错误应该通过修复第二个错误来更改。但请注意,您在这里所做的是复制函数返回的指针,而不是指针指向的content。最简单的方法是将tmp的定义更改为char *tmp;,而不是保留temp返回的指针。