We explore all kinds of technologies, starting from programming, electronics ending in the photography sector. A bit for everyone.

Friday, September 22, 2017

Arduino: Char Array Problems

While programming an embedded system based on ATmega328p, I have encounter some char array problems in the C programming language.


I had a char array of exactly 8 chars ending with /0:
char a[] = "///////0";
It had trouble reading a[0] many times. For some still unknown reason, when I had:
char a[] = "10000000";
it would corrupt some memory addresses close to the array and it never work as I wanted. The problem is not the '/0' at the end. '/0' it's NOT the null terminator (NUL character in ASCII). The null terminator is '\0'. I tried this with both, the C compiler in the Arduino's native IDE and the C compiler in the Atmel Studio 7 (with the Visual Micro installed) and it was the same exact thing. So, what's wrong? I still don't know, but I'm writing this blog post, because I fixed the problem by adding a random character in the front and in the back that would be never read by any actual code. Just for storing purposes. I can't reveal the whole code, but because of the trouble that I went through I don't recommend using char arrays now at all... :D

The solution code for me was:
char a[] = "/10000000/";

- Mitko
Share:

0 comments:

Post a Comment

Contributors

Powered by Blogger.