【发布时间】:2017-09-19 02:39:33
【问题描述】:
我正在用 C 语言编写 Arduino 板,因此除非我与外部终端窗口进行串行通信,否则无法打印。
因此我开发了一个 printAll 方法:
void printAll(char * str) {
int i;
for(i = 0; str[i] != 0x00; i++) {
PSerial_write('0', str[i]);
}
}
我还有一个变量:
char input[12];
input[0] = 'h';
input[1] = 'e';
input[2] = 'l';
input[3] = 'p';
我想做的是将此数组传递给 printAll 方法(但 printAll 方法需要一个 char*)。
我已经尝试过:
printAll(&input[0]);
但是什么都没有打印出来!但是当我逐步打印输入数组的每个字符时,我得到:
help<0><0><0><0><0><0><0><0>
谁能解释为什么这不起作用?谢谢!
***注意:当这样使用时,printAll 方法工作得非常好:
printAll("Hello World!");
总的来说,我的代码如下所示:
char input[12];
int main(void) {
start();
}
void start() {
while(1) {
printAll("Please enter in a command!\r");
printAll("Please type 'help' for instructions!\r");
char input[12];
readInput(input);
printAll("Trying to print the char array stuff....\r");
printAll(input);
if (input == "help") printHelp();
else if (input == "set") {
if (setStatus) printSet();
else printAll("Controller not authorized to print.\n");
}
else if (input == "set on") setStatus = true;
else if (input == "set off") setStatus = false;
else if (input == "set hex=on") hexFormat = true;
else if (input == "set hex=off") hexFormat = false;
else if (input == "set tlow") tlow = getNumber(input);
else if (input == "set thigh") thigh = getNumber(input);
else if (input == "set period") period = getNumber(input);
x_yield();
}
}
void readInput() {
char c = PSerial_read('0'); //reads character from user
while (c != '\r') {
//while the character isnt 'enter'
input[currIndex] = c;
c = PSerial_read('0');
currIndex++;
}
int y;
for(y = 0; y < 12; y++) {
PSerial_write('0', input[y]);
//go through input and print each character
}
PSerial_write('0', '\r');
//add new line to print log
currIndex = 0; //reset for next input (overwrites current stuff)
}
现在无论我输入什么,它只是要求更多的输入,并且在输入法返回后从不打印数组。
【问题讨论】:
-
在打印之前也做
input[4] = 0;
标签: c arrays pointers arduino char