Pages: [1]   Go Down
Author Topic: Possible bug with global char[]  (Read 571 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Online Online
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strings are null terminated in C...

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not if you tell the compiler is just a collection of characters instead of a string.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: