Reputation: 1151
I am working on programming an Arduino board in C, and thus cannot print unless I do serial communication to an external terminal window.
Because of this I have developed a printAll method:
void printAll(char * str) {
int i;
for(i = 0; str[i] != 0x00; i++) {
PSerial_write('0', str[i]);
}
}
I also have a variable:
char input[12];
input[0] = 'h';
input[1] = 'e';
input[2] = 'l';
input[3] = 'p';
What I would like to do is pass this array into the printAll method (but the printAll method takes a char*).
I have tried to do:
printAll(&input[0]);
But nothing gets printed! But when I step through and print each character of the input array I get:
help<0><0><0><0><0><0><0><0>
Can anyone explain why this is not working? Thanks!
***NOTE: the printAll method works perfectly fine when used like so:
printAll("Hello World!");
Overall my code looks like this:
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)
}
Right now no matter what I input, it just asks for more input, and never prints the array out after the input method returns.
Upvotes: 2
Views: 972
Reputation: 8020
The code that you've sent is mixed and does not compile. The code suggests that you have two input variables, one global and one local to main. readInput() reads the global one and printAll() the local one (or vice versa, depending on which code was changed). Remove the global input, that should not be used anyway and pass the same input variable to both readInput() and printAll().
Upvotes: 2
Reputation: 380
I don't know much about Arduinos, so bear with me here.
In your readInput
function, you didn't include any parameters for the input
array, which means that you're using the global variable declared at the beginning of your code in this function. However in your start
function, you declare another input
array and pass it to readInput
, even though the function doesn't take any arguments.
This new input
will actually "hide" the global input
variable for the function it was declared in. This means that start
uses this local input
even though readInput
uses the global input
. In other words, your actually using two different variables, not one.
To fix the problem, try removing either the char input[12]
in start
, or remove the global char input[12]
at the beginning of your code and add it as a parameter for readInput
: void readInput(char * input)
.
Note: Even though you didn't include any parameters in the readInput
, the C compiler won't complain if pass arguments unless you place void
in the parenthesis: void readInput(void)
.
Upvotes: 1
Reputation: 44
Notice that the string literal "Hello World!" is a char[]. The fact that it prints corectly meanse that something else is wrong with your code. Please post the whole code you're trying to run.
Also, notice that char[] is converted automatically to char * when referenced. This is called array decaying into pointer. See:
Why do arrays in C decay to pointers?
So, both:
printAll(&input[0]);
And
printAll(input);
Do the same thing: they convert input to a char *.
EDIT: from your larger code I can at least see that you're not adding a '\0' character after reading and are not sending a '\r' when printing. String comparisons are also incorrect and should be done with strcmp().
Upvotes: 1