我正在为微 Controller 编写 uart 代码。我的代码在传递 string("...") 时工作正常,但是当我尝试传递 char('.') 时出现问题。是的,我已经将“s”声明为字符串指针,但有可能传递 char 值。是否有可能在 send_data 中同时传递字符串和字符值?
#include <stdio.h>
void send_data( char *s)
{
while(*s!='\0')
{
send_dt1(*s);
s++;
}
}
void send_dt1( char in_c)
{
printf("%c",in_c);
}
int main(void)
{
send_data("sample_data"); //fine
send_data('Q'); //warning, no data displaying
return 0; /* terminate the program */
}
谢谢..
最佳答案
由于 send_data
期望 s
无论如何都是一个 null-terminated 字符串,我只推荐使用一个字符串:
send_data("Q");
注意事项:
- 只要不修改
s
处的字符串,就应该传递一个const char*
关于C - 传递 'send_data' 的参数 1 从整数生成指针而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611122/