atoi quirks?

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

I've been encountering a lot of problems trying to do serial communication with a digital compass device, and have noticed after experimenting for hours that if I run "atoi" on the same char string containing the heading info 3 times in a row (i.e. 3 lines right after the other doing the same atoi into 3 different variables) get different results. The last two results are correct, but the first result oscillates between the correct value and some strange erroneous value every now and then. Why is this? Here's a code snippet of what I'm talking about...

		Param1DegStr[0] = UART1Buffer[0];
		Param1DegStr[1] = UART1Buffer[1];
		Param1DegStr[2] = '\0';
		Heading1 = atoi(Param1DegStr);
		Heading2 = atoi(Param1DegStr);
		Heading3 = atoi(Param1DegStr);

The values Heading2 & Heading3 will consistantly read the same number, i.e. 17, however, Heading1 will show 17 most of the time but sometimes show some strange value like 12596 instead?@? I've just got a break point right after all of the "Headingx = " statements and expected to at least see that they all get the same result, but when I saw that they don't have wondered if the problem lies with atoi itself? Am I calling it too soon after making the assignment into my char string or something?

Thanks,
James

Last Edited: Sun. May 13, 2007 - 07:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry...... I just discovered the problem.

I was over-writing an array someplace else in my code. Everything is working fine now... Sorry!

- James