Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将uint16\u t变量打印到4个位置的功能将不起作用_C_Arduino_Char - Fatal编程技术网

C 将uint16\u t变量打印到4个位置的功能将不起作用

C 将uint16\u t变量打印到4个位置的功能将不起作用,c,arduino,char,C,Arduino,Char,平台:ESP8266和Arduino 我正在尝试以4个字符的位置输出uint16\t。laserVL53L0X正在读取2到4个数字位置的读数。永远不会超过4个位置,最多8190个 Serial.print( mmLaser); 可以工作,但无法设置4个位置的格式。 如果我调用这个函数,我会得到一个错误 **ERROR:** invalid conversion from 'char' to 'char*' [-fpermissive] 如果我编译而不调用函数:没有错误 我做错了什么 声明变量

平台:ESP8266和Arduino

我正在尝试以4个字符的位置输出uint16\t。laserVL53L0X正在读取2到4个数字位置的读数。永远不会超过4个位置,最多8190个

Serial.print( mmLaser);
可以工作,但无法设置4个位置的格式。 如果我调用这个函数,我会得到一个错误

**ERROR:** invalid conversion from 'char' to 'char*' [-fpermissive]
如果我编译而不调用函数:没有错误

我做错了什么

声明变量

调用的函数

作用


代码需要一个字符数组

//       Pointer to a character -----v                            
void uint16_2_char(uint16_t n, char* c){
  sprintf( c, "%04d", (int) n );
}
问题代码

//     This is one character
char  c_temp;

uint16_t mmLaser = 0;  // hold laser reading

// **ERROR:** invalid conversion from 'char' to 'char*'
// Does not make sense to pass a character when an address is needed
// Need to pass the initial _address_ as an array of characters instead.
//                         v
uint16_2_char( mmLaser, c_temp); 
更好的代码

#define INT_BUF_SIZE 24 
char buffer[INT_BUF_SIZE]; 

// When an array is passed to a function, 
// it is converted to the address of the 1st character of the array.
//  The function receives &buffer[0]
uint16_2_char( mmLaser, buffer); 
更好的是,传递一个地址和可用的大小

void uint16_2_char(uint16_t n, char* c, size_t sz){
  unsigned u = n;
  // I'd expect using unsigned types.  (use `%u`)
  // snprintf() will not not overfill the buffer
  snprintf( c, sz, "%04u", u);
}

char buffer2[INT_BUF_SIZE]; 
uint16_2_char2( mmLaser, buffer, sizeof buffer); 

关于将char传递给需要char*的函数,编译器会怎么说?char c_temp;->煤焦温度[100];我猜我不相信“永不在”永远不会超过4个位置。最好将缓冲区的大小调整到整数所能产生的最大值。我发现自己奇怪地感到不安,因为我指出无符号整数没有小数位数。这是一个奇怪的问题拼图,这是没有帮助的。请阅读。把它放在心上。提供一个4位限制设置,因为激光读数最大为8190毫米,覆盖激光时最小为18-您是正确的,它们是整数而不是小数%04d不是最大限制。4是将打印到字符串的字符宽度的最小限制。这将需要至少5个字符。根据平台和未来,编码的函数需要7个字符。为什么要冒缓冲区溢出的风险?请慷慨地分配缓冲区。
//     This is one character
char  c_temp;

uint16_t mmLaser = 0;  // hold laser reading

// **ERROR:** invalid conversion from 'char' to 'char*'
// Does not make sense to pass a character when an address is needed
// Need to pass the initial _address_ as an array of characters instead.
//                         v
uint16_2_char( mmLaser, c_temp); 
#define INT_BUF_SIZE 24 
char buffer[INT_BUF_SIZE]; 

// When an array is passed to a function, 
// it is converted to the address of the 1st character of the array.
//  The function receives &buffer[0]
uint16_2_char( mmLaser, buffer); 
void uint16_2_char(uint16_t n, char* c, size_t sz){
  unsigned u = n;
  // I'd expect using unsigned types.  (use `%u`)
  // snprintf() will not not overfill the buffer
  snprintf( c, sz, "%04u", u);
}

char buffer2[INT_BUF_SIZE]; 
uint16_2_char2( mmLaser, buffer, sizeof buffer);