help: really weird problem with char and arrays...

hi everyone

I have this really weird problem right now and I would be very glad
if anyone could look at my code...

The code parses a bluetooth adress that is recieved over the serial connection.
As you can see, I have an array listOfDevices, that stores 5 device adresses.
Now my problem is that after I parse the bluetooth code there is a change
in this array that shouldn't be: the first item of the array changes to the
newest parsed adress, even though I didn't change the item.

The change really happens between the first and the second time I print out the adress.
Do you have any idea why this could happen?

I'm thankful for any help with this...

Yves

char device_adress[18];
char* listOfDevices[7]; // all the listOfDevices that may connect to the power thingy

if (message[0] == char(82) && message[1] == char(73) && message[2] == char(78) && message[3] == char(71)){
// RING, device tries to connect
Serial.println("RING RING RING");

Serial.println("device list start:");
Serial.println(listOfDevices[0]); ///////////// shows the latest adress correctly
Serial.println(listOfDevices[1]);
Serial.println(listOfDevices[2]);
Serial.println(listOfDevices[3]);
Serial.println(listOfDevices[4]);
Serial.println("device list end:");

device_adress[0] = message[7]; // parsing the bluetooth adress
device_adress[1] = message[8];
device_adress[2] = message[9];
device_adress[3] = message[10];
device_adress[4] = message[11];
device_adress[5] = message[12];
device_adress[6] = message[13];
device_adress[7] = message[14];
device_adress[8] = message[15];
device_adress[9] = message[16];
device_adress[10] = message[17];
device_adress[11] = message[18];
device_adress[12] = message[19];
device_adress[13] = message[20];
device_adress[14] = message[21];
device_adress[15] = message[22];
device_adress[16] = message[23];

Serial.println("device list start:");
Serial.println(listOfDevices[0]); suddenly shows device_adress, even though I didn't assign it to this array index?!?
Serial.println(listOfDevices[2]);
Serial.println(listOfDevices[3]);
Serial.println(listOfDevices[4]);
Serial.println("device list end:");

It would be better if you posted the whole program using the "code" key (#). Particularly the assignments to "listOfDevices" and the declaration of "message"

Have you got something against using "for" loops? :wink:

Also:

message[0] == char(82) && message[1] == char(73) && message[2] == char(78) && message[3] == char(71)

is easier to read as:

message[0] == 'R' && message[1] == 'I' && message[2] == 'N' && message[3] == 'G'

Not all of us speak ASCII!

hi!

no, I actually like for loops :). It was just a very quick thing to nail down
where the problem was...

hm the code is too long to post here, I think but I'll add the assignments here:

the assignment for message:

char message[128];

the code where the addresses are assigned:

           if (known_device == false){ // adding device to the list
                    
                    device_count = 0; // finding a free spot for the new device adress
                    while(listOfDevices[device_count][0] != '?')// no entry
                    {
                    device_count ++;
                    }

                    listOfDevices[device_count] = device_adress;
                    
                    Serial.println("device added to the list");
                    bluetoothBlink();
                    friendlyDeviceAround = true;
                    led_bluetoothOn = 1;
                    
                }// end if

What does "device_adress" look like?

it's a MAC adress that looks for example like this:
01:23:45:67:89:ab

thanks for looking at my code, I'm really getting desperate here...

No, I meant, "what does the declaration of "device_adress" look like?"

You have a list of pointers to device addresses (listOfDevices) , but I don't see the allocation of memory for all these addresses.

do you mean that bit?

char device_adress[18];

I wonder if the symptoms you are seeing are because you have run out of RAM. If you have a lot of strings being printed to the serial port, this along with the buffers you are explicitly allocating, may be causing the problem. Try commenting out all Serial.print code except that needed to verify that the address is correct.

hi there

I followed your advice and commented most of the debug serial communication out.
no luck though so far. I have four unsigned longs in use (for timer uses), could they
cause trouble?

I'm using an arduino mini by the way...

thanks so much for your help!
yves

I'd probably go with that, mem.

Sorry about the misunderstanding about "device_adress" - what I really meant was, "listOfDevices" is an array of pointers, but where is the memory that holds whatever it is that these pointers point to?
Where and what to you assign to, say "listOfDevices [0]" ?

ok, we are getting there I think...!
as far as I can tell, I didn't allocate any memory to them.

I tried to do it like this:

char* listOfDevices[7][18];

This is accepted, but if I try to compare like this:

 if (listOfDevices[device_count][0] == device_adress[0]){

It's not accepted because of this error:
In function 'void parseSerial()':
error: ISO C++ forbids comparison between pointer and integer In function 'void emptyDeviceList()':

So how would I do that?
yves

 if (listOfDevices[device_count][0] == device_adress[0]){

It's not accepted because of this error:
In function 'void parseSerial()':
error: ISO C++ forbids comparison between pointer and integer In function 'void emptyDeviceList()':

So how would I do that?
yves

You need to make the pointer, an int.
The way that is done is by 'dereferencing' the pointer as per:

if ( *listOfDevices[device_count][0] == device_adress[0] ){ 
  //
}

As I can understand what is happening here:

  • There is only one device_adress[] char array.
  • The latest adress received is copied into this from the message.
  • The listOfDevices[device_count] is assigned to this adress

The problem is that ALL the entries in listOfDevices will point to the same device_adress containg the latest adress.

You must declare listOfDevices as 7 character arrays and copy the adress data into them, something like:

char listOfDevices[7][18];
 ...
strncpy(listOfDevices[device_count],&message[7],17);