Need some help with passing arrays

Hi all,

I'm new here and not real good in C programming. Thanks in advance for any suggestions. Below is a code snippet that I am stuck on. I've been trying to read up on it, but I am still..... stuck!

//With inputarray, I want to be able to pick how many and which arrays I want to DoThis.  
// inputarray[0] says how many bytes are in the array. The following bytes in inputarray I want to append to variable name Var_ and then DoThis

void myfunction(unsigned char* inputarray)
{
  // These arrays are predefined
  uint8_t Var_1[8] = {0x28, 0xF4, 0x9B, 0x95, 0x05, 0x00, 0x00, 0x22};
  uint8_t Var_2[8] = {0x28, 0x6D, 0x14, 0x96, 0x05, 0x00, 0x00, 0x29};
  uint8_t Var_3[8] = {0x28, 0x4B, 0x66, 0x96, 0x05, 0x00, 0x00, 0xC3};
  uint8_t Var_4[8] = {0x28, 0x5E, 0x70, 0x96, 0x05, 0x00, 0x00, 0x93}; 
  //Continue with Var_ arrays defines
  
   for(int i=0; i<inputarray[0]; i++) 
  {
    //Here is my problem. This function passes an 8 byte array. 
    DoThis(Var_ + inputarray[1+i]); //obviously this doesn't work, but kind of shows what I am trying to do
  }
}

Again, I appreciate any suggestions you give me.
Carey

EDIT: Changed the subject to better reflect the issue

I'm afraid I can't understand what you're trying to do or what your problem is from the snippet you posted. Do you need to know how to pass an array pointer to a function?

Post your actual code, or pare down your code to have the minimum that still shows your problem..

I'm sorry if I wasn't clear enough. I need to pass the actual array (Var_1, Var_2, etc) to the DoThis(uint8_t*) function. Maybe my subject is misleading.

Carey

one way to "fake" a 2-dimensional array is to just stack the "rows" end to end
array 1 = abcde
array 2 = defgh
array 3 = ijklm

so, define it like myarray[9]="abcdefghijklm"

and access it as a subset something like
define passer[5]
for row = 0 to 2
for col = 0 to 4
passer[col] = myarray[row*5+col]
next
do function call with passer now
next

the multiplier you use on row is the size of the sub arrays (5 in this case)
there may be a way to pass a specific range of myarray but I don't know what it would be offhand. something to tell it to pass myarray[row*5+col,5] etc

if you need to access rows from such an array frequently, you might want to wrap up that inner for loop into a separate function for just that, to save yourself a bit of headache later.

Thanks for the reply. I think I follow what you are suggesting. Showing my ignorance, I was hoping for something a little more straight forward maybe.

Maybe others could offer suggestions.

Thanks again!
Carey

I was just noticing arduino supports multidimensional arrays.
char myarray[5][3]
etc. search for "arduino two dimensional arrays", there are many hits with different applications, find what works for you

Thanks again for the suggestions virtual1! Tell me if you think this would work.

void myfunction(unsigned char* inputarray)
{
  // Build 2D Array instead of 4 1D arrays 
  uint8_t Vars[4][8] = {{0x28, 0xF4, 0x9B, 0x95, 0x05, 0x00, 0x00, 0x22},
                        {0x28, 0x6D, 0x14, 0x96, 0x05, 0x00, 0x00, 0x29},
                        {0x28, 0x4B, 0x66, 0x96, 0x05, 0x00, 0x00, 0xC3},
                        {0x28, 0x5E, 0x70, 0x96, 0x05, 0x00, 0x00, 0x93}}; 
  
   //inputarray[0] says how many times to DoThis
   for(int i=0; i<inputarray[0]; i++) 
  {
    //inputarray[1], inputarray[2], etc tells what row of data to use for DoThis
    DoThis(Vars[inputarray[1+i]][8]); //Will this work???
  }
}

Carey

well you can access it one of a couple wayss. you could access individual elements with Vars[row][column], or you could pass entire row arrays into functions with Vars[row]

remember that arrays are zero-based, so [4][8] has rows ranging from 0-3, and columns from 0-7

A 2-dimensional array is just an array of arrays. but you can also have arrays of classes. depending on what you're trying to do, that may be more useful:

Great info! So my code gets more simple...

void myfunction(unsigned char* inputarray)
{
  // Build 2D Array instead of 4 1D arrays 
  uint8_t Vars[4][8] = {{0x28, 0xF4, 0x9B, 0x95, 0x05, 0x00, 0x00, 0x22},
                        {0x28, 0x6D, 0x14, 0x96, 0x05, 0x00, 0x00, 0x29},
                        {0x28, 0x4B, 0x66, 0x96, 0x05, 0x00, 0x00, 0xC3},
                        {0x28, 0x5E, 0x70, 0x96, 0x05, 0x00, 0x00, 0x93}}; 
  
   //inputarray[0] says how many times to DoThis
   for(int i=0; i<inputarray[0]; i++) 
  {
    //inputarray[1], inputarray[2], etc tells what row of data to use for DoThis
    DoThis(Vars[inputarray[1+i]-1]); //This passes a whole row?? The -1 compensates for the 0 reference of arrays. Will this work???
  }
}

Thanks again!
Carey