我正在C编程Arduino板,因此无法打印,除非我做串行通信到外部终端窗口。如何从c中的char *访问char []?
因为这一点,我已经开发出一种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("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)
}
现在不管我输入时,它使用像这样当printAll方法工作完全正常只是要求更多的输入,并且在输入方法返回后决不会输出数组。
也做'input [4] = 0;'在打印 –