Go Down

Topic: How to pass Byte_Array[29] into method/function call? (Read 607 times) previous topic - next topic

Mavromatis

I'm trying to optimize my code and can't figure out how to send a byte array into a method.

Example:

Code: [Select]
void doSomething(){
   
   byte my_array[29] = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
        0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00
    };

    doingSomething(my_array);
     
}

void doingSomething(byte input_array){
   // do something with the input_array
}


Any ideas?

PaulS

Code: [Select]
void doingSomething(byte input_array){
This function is defined to take a byte, not a byte array.

Code: [Select]
void doingSomething(byte input_array[])
{ // Down here where it belongs!

This function is defined to take an array.

Mavromatis

Tried that but when I do:

Code: [Select]
void doSomething(){
   
   byte my_array[29] = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
        0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00
    };

    doingSomething(my_array);
     
}

void doingSomething(byte input_array[]){
   // do something with the input_array
   Serial.println(sizeof(input_array), DEC);
}


The sizeof doesn't give the correct size.  hmmm...

PaulS

Quote
The sizeof doesn't give the correct size.  hmmm...

Of course not, because what really got passed was a pointer to the start of the array. If you want the function to know the length of the block of memory pointed to, you must also tell it that, typically by telling it the number of elements in the array.

I should mention that sizeof() DID get the correct size - it just wasn't the size you expected.

Mavromatis

#4
May 02, 2012, 01:51 am Last Edit: May 02, 2012, 01:52 am by Mavromatis Reason: 1
like: doingSomething(my_array, sizeof(my_array));

void doingSomething(byte input_array[], int arraySize){   
} // do something with the input_array

??

winner10920

If the size is known its easy to use pointers, search reference and deference
I had to learn them when I was working with byte arrays myself, after learning how to use them properly its pretty easy
however explainging them is beyond me maybe someone else or google will help


Go Up