Null character at the start of the string, not end.

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder if it has something to do with the way the string is fetched via RS232?

char receiveChar()
{
	// Return the char in the UDR0 register
	return UDR0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He's parsing the string, then clearing the string buffer in preparation to receive the next string.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this line erases all the elements in the array?

string[0] = '\0';

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

along with the count = 0, yes. (in this context)

It puts a zero length string into the buffer. It doesn't remove or erase anything but the first character.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks chuck,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a safety feature. The first element of the string points to a memory location that is supposed to be a null terminated string. If the string gets stepped on, that is if an error occurs somewhere in the code and the string memory gets corrupted, then when that string pointer is provided to a function that expects to read a string, it will read the data until it finds a null which might be thousands of bytes down the line or never. By putting an null in the first character in an empty string that you are finished with you help assure that in an error condition the string function will terminate. This method isn't needed for programs with no bugs, but that's a bet I usually won't take.

This also helps if you are using strLen() and only reacting if it returns non-zero. The string with the null in the first element will return zero, so you know it is an empty string and you don't need to deal with it.

Smiley