Pages: [1]   Go Down
Author Topic: How to pass Byte_Array[29] into method/function call?  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA
Offline Offline
Full Member
***
Karma: 0
Posts: 194
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Example:

Code:
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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46235
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
void doingSomething(byte input_array[])
{ // Down here where it belongs!
This function is defined to take an array.
Logged

Seattle, WA
Offline Offline
Full Member
***
Karma: 0
Posts: 194
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tried that but when I do:

Code:
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...
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46235
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA
Offline Offline
Full Member
***
Karma: 0
Posts: 194
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

like: doingSomething(my_array, sizeof(my_array));

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

??
« Last Edit: May 01, 2012, 06:52:43 pm by Mavromatis » Logged

Offline Offline
Edison Member
*
Karma: 4
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA
Offline Offline
Full Member
***
Karma: 0
Posts: 194
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

got it! it worked.
Logged

Pages: [1]   Go Up
Jump to: