I am trying to figure out why a null character is used in a certain way that I've not seen before. The code is from smileys rs232 code, where he fetches a string typed into hyperterminal.
After the string is received it is parsed through a separate function, then the first element of the string storage array is replaced with a null character. why is there a need for a null character for the first element when it will be overwritten regardless?
i think i'm missing something really basic here
:oops:
Anyone care to take a look? thanks?!, here is the code:
void parseInput(char s[]) { // parse first character switch (s[0]) { case 'c': if( (s[1] == 'o') && (s[2] == 'm') && (s[3] == 'm') ) switch (s[4]) // parse the fifth character { case 'a': Comm1(s); break; case 'b': Comm2(s); break; case 'c': Comm3(s); break; case 'd': Comm4(s); break; default: sendString("\rYou sent: '"); sendChar(s[0]); sendString("' - I don't understand.\r"); } break; case 'd': if( (s[1] == 'e') && (s[2] == 'm') && (s[3] == 'o') && (s[4] == '?') ) sendString(s); sendString("You are talking to the PC_Comm demo.\r"); break; case 'h': if( (s[1] == 'e') && (s[2] == 'l') && (s[3] == 'l') && (s[4] == 'o') ) sendString("Hello yourself\r"); DDRD = 0xff; PORTD = 0Xff; break; default: sendString("\rYou sent: '"); sendChar(s[0]); sendString("' - I don't understand.\r"); break; } s[0] = '\0'; } int main(void) { char string[64]; unsigned char count = 0; // run the initialization routine initializer(); //Begin forever chatting with the PC for(;;) { // Check to see if a character is waiting if( isCharAvailable() == 1 ) { // If a new character is received, get it string[count++] = receiveChar(); // receive a packet up to 64 bytes long if(string[count-1] == '\n')// Hyperterminal string ends with \r\n { string[count-2] = '\0'; //convert to a string parseInput(string); string[0] = '\0'; count = 0; } else if(count > 64) { count = 0; string[0] = '\0'; sendString("Error - received > 64 characters"); } } } return 0; }