简体   繁体   English

从char到const char的转换无效*

[英]Invalid conversion from char to const char*

Sorry in advance for what I feel is going to be a really simple problem, but I've been stuck on it for hours and I haven't been able to work out how to fix it based on the stuff I've found here or on google. 提前抱歉,我认为这将是一个非常简单的问题,但我已经坚持了几个小时,我无法根据我在这里发现的东西找出解决方法在谷歌上。

I've got an arduino hooked up to a GPS and a radio and am trying to broadcast the GPS signal to my radio. 我有一个arduino连接到GPS和收音机,我正试图将GPS信号广播到我的收音机。 What I am trying to do now is get the NMEA sentence from the GPS into the variable 'text', but am getting confused by this error, which I think is due to arrays. 我现在要做的是将GPS中的NMEA句子转换为变量“text”,但我对此错误感到困惑,我认为这是由于数组造成的。

My error is occurring on this line: sprintf(text, char(c)); 我的错误发生在这一行: sprintf(text, char(c));

I've tried a few different things but this is where I'm stuck at the moment. 我尝试了一些不同的东西,但这是我现在卡住的地方。 Any help would be really appreciated. 任何帮助将非常感激。

#define RADIOPIN 13
#include <string.h>
#include <util/crc16.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);

#define GPSECHO  true

Adafruit_GPS GPS(&mySerial);

char datastring[80];
char text[80];

void setup() {
  Serial.begin(115200);
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
  delay(3000);  
  pinMode(RADIOPIN,OUTPUT);
}

void loop(){
  gpscheck();
}

void gpscheck(){
    char c = GPS.read();
    if (c) {
//      Serial.print(c); 
    sprintf(text, char*(c));
    Serial.print(text);
    }
}

You might want to read a reference of sprintf . 您可能想要阅读sprintf的参考。 Then you will see that the second argument is a string . 然后你会看到第二个参数是一个字符串

So the following would be fine: 所以以下情况会很好:

sprintf(text, "%c", c);

To be on the safe side, you might want to use snprintf instead, to lessesn the risk of buffer overflows. 为了安全起见,您可能希望使用snprintf来避免缓冲区溢出的风险。

Of course, for a single character, you might as well do eg 当然,对于单个角色,你也可以这样做

text[0] = c;
text[1] = '\0';  /* Terminate string */
sprintf(text, char*(c));

If you want to print character 如果你想打印字符

simply use printf("%c",c);

if you want to copy that into text 如果你想将其复制到文本中

sprintf(text,"%c", c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM