Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: March 21, 2011, 01:50:57 am » |
Hi, I am experiencing a weird bug with char[] declared in the global section. The code below does something odd where it appends the contents of the FSecond array to the end of FFirst, but FSecond remains fine. Is this something that is expected for C? (I'm a delphi guy) This seems like an odd bug to me. Running on Arduino UNO SMD, IDE 0022 Code: char FFirst[] = {20, 96, 21, 114, 22, 88, 23, 5}; char FSecond[] = {8, 160, 5,183 ,8 ,132 ,166, 5, 140 ,154 ,146};
void setup() { Serial.begin(9600); AnnounceEmail(); }
void loop() {
}
void AnnounceEmail() { Serial.println(FFirst); Serial.println(FSecond); }
|
|
|
|
« Last Edit: March 21, 2011, 02:02:07 am by SimRacer »
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10142
|
 |
« Reply #1 on: March 21, 2011, 02:28:15 am » |
Strings are null terminated in C... char FFirst[] = {20, 96, 21, 114, 22, 88, 23, 5, 0}; char FSecond[] = {8, 160, 5,183 ,8 ,132 ,166, 5, 140 ,154 ,146, 0};
They are also null terminated in Delphi but the compiler / run-time library makes it so you don't need to know that.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #2 on: March 21, 2011, 02:42:56 am » |
Thanks. I am well aware of the null terminated strings in Delphi, but this sort of thing should really be implemented in a compiler these days. Guess I should not assume.
|
|
|
|
« Last Edit: March 21, 2011, 03:08:05 am by SimRacer »
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #3 on: March 21, 2011, 03:08:30 am » |
Not if you tell the compiler is just a collection of characters instead of a string.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #4 on: March 21, 2011, 03:43:32 am » |
If I declare it as two separate variables then it should be treated as such. Do the same in MPLAB, Delphi, C# etc and it will give you the predicted behavior that is common, which is defined as a char array of the length it determines at compile time.
|
|
|
|
« Last Edit: March 21, 2011, 03:52:03 am by SimRacer »
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6815
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #5 on: March 21, 2011, 03:58:03 am » |
All C string funcs work on an array of characters with a \0 at the end. Print (FFirst) will start with the first character of FFirst and walk though memory until it sees a \0. In your case that was somewhere random after the end of FSecond. You were lucky it stopped when it did, it could have tried to print the entire contents of RAM  You either have to manually put the \0 in as CB showed, or declare it as a string char myString[] = "Hello"; or a pointer to a string char *myString = "Hello"; You have control characters so that makes it harder. If they are standard escapable chars you can embed them in the string, if not doing what you're doing is probably the best way. Remember C is only one step up from assembler, that's it's power, but you don't get as big a safety net as you may be used to. ______ Rob
|
|
|
|
« Last Edit: March 21, 2011, 04:02:23 am by Graynomad »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #6 on: March 21, 2011, 04:18:12 am » |
Yeah I get your point. I'm pretty new to C and didn't think the functions work in this fashion, assuming the terminating char was implicit.
My chars are not standard escape chars, they are numbered commands for a chip it talks to serially. So this is the way I have to do things, however with this knowledge about how the functions work I can use it to my advantage when wanting to build the message to send to the chip.
Cheers.
|
|
|
|
« Last Edit: March 21, 2011, 04:23:54 am by SimRacer »
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 71
Posts: 6603
Arduino rocks
|
 |
« Reply #7 on: March 21, 2011, 06:25:13 am » |
If your chip could validly expect a null char, you shouldn't be passing a string, you should be passing a length and an array. Strings are human readable, commands to a chip are raw binary (at least that's my abstract take on such things).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #8 on: March 21, 2011, 06:28:58 am » |
If your chip could validly expect a null char, you shouldn't be passing a string, you should be passing a length and an array. Strings are human readable, commands to a chip are raw binary (at least that's my abstract take on such things).
yeah i only pass in the array. It uses the soft serial class and just calls the print function and passes the char array. However the strings are a useful tool to concat the char arrays together?
|
|
|
|
« Last Edit: March 21, 2011, 06:30:43 am by SimRacer »
|
Logged
|
|
|
|
|
|