2017-04-22 43 views
2

我正在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方法工作完全正常只是要求更多的输入,并且在输入方法返回后决不会输出数组。

+1

也做'input [4] = 0;'在打印 –

回答

2

您发送的代码是混合的并且不会编译。代码表明你有两个输入变量,一个是全局变量,一个是本地变量。 readInput()读取全局变量和printAll()本地变量(反之亦然,取决于哪个代码已更改)。删除全局输入,不应该使用全局输入,并将相同的输入变量传递给readInput()和printAll()。

+0

是的,它会做到这一点。 idk我错过了那个,感谢你的好眼睛! – skyleguy

+0

虽然我有你,但是......它正确地打印我输入的内容,但是如果它匹配if语句块中的关键字,它不会执行它们。我没有使用正确的比较来检查输入和这些字符串文字之间的相等性吗? JK我只是做了一个快速搜索,发现strcmp做的工作 – skyleguy

+0

if(strncmp(input,“help”,sizeof(input))== 0)printHelp(); – hdante

1

请注意,字符串文字“Hello World!”是一个char []。它真正打印的事实意味着你的代码有其他问题。请发布您尝试运行的整个代码。

此外,请注意,char []会在引用时自动转换为char *。这被称为数组衰减成指针。请参阅:

Why do arrays in C decay to pointers?

所以,既:

printAll(&input[0]); 

而且

printAll(input); 

做同样的事情:它们将输入到一个char *。

编辑:从您的较大的代码我可以至少看到,你没有添加一个'\ 0'字符后阅读和打印时不发送'\ r'。字符串比较也是不正确的,应该用strcmp()完成。

+0

之前编辑它。这是我迄今为止所有的。我确实有if/else if语句使用的方法,但代码没有得到那么多,所以我看不到需要发布它们 – skyleguy

+0

我已经添加了一些注释,但我建议先从一个干净的代码开始,然后只定义阵列并打印它。 – trollkill

1

我对Arduinos了解不多,所以请在这里忍受。

在您的readInput函数中,您没有包含input数组的任何参数,这意味着您正在使用在此函数的代码开头声明的全局变量。但是,在start函数中,即使该函数没有使用任何参数,也可以声明另一个input数组并将其传递给readInput

这个新input实际上将“隐藏”了它在声明的功能全局input变量。这意味着start使用这个地方input即使readInput使用全局input。换句话说,你实际上使用了两个不同的变量,而不是一个。

要解决该问题,请尝试去除start无论是char input[12],或者在你的代码的开头删除全局char input[12]并将其添加为一个参数为readInputvoid readInput(char * input)

注:void readInput(void):即使你没有包括在readInput任何参数,C编译器不会,如果传递参数,除非你放在括号void抱怨。

+0

我不知道c编译器!这将有益于未来的感谢! – skyleguy

+0

尽管我有你,但它...正确地打印了我输入的内容,但是如果它匹配if语句块中的关键字,它不会执行它们。我没有使用正确的比较来检查输入和这些字符串文字之间的相等性吗? JK我只是做了一个快速搜索,发现strcmp做这个工作 – skyleguy

+2

当你比较一个字符串时,你必须使用'strcmp()'函数。使用'=='运算符将把数组视为一个指针。 –