Go Down

Topic: Passing and returning Arrays into functions (Read 559 times) previous topic - next topic

psppb

I am trying to pass an array of binary numbers into a function and (eventually) have it return the updated array. I keep getting some errors I do not understand can someone tell me what to do? I have the function and inputs in the code (snippets where the errors are flagged) and the errors posted below

[font=Verdana]Cleverblocks_test_2.cpp: In function 'void loop()':
Cleverblocks_test_2:229: error: expected primary-expression before ']' token
Cleverblocks_test_2:229: error: expected primary-expression before ']' token
Cleverblocks_test_2:229: error: expected primary-expression before ']' token
Cleverblocks_test_2:229: error: expected primary-expression before ']' token
Cleverblocks_test_2.cpp: In function 'void CheckBlocks(int*, int*, int*, int*)':
Cleverblocks_test_2:257: error: expected primary-expression before ']' token
Cleverblocks_test_2:257: error: 'Bank1' was not declared in this scope
Cleverblocks_test_2:257: error: expected primary-expression before ']' token
Cleverblocks_test_2:259: error: expected primary-expression before ']' token
Cleverblocks_test_2:259: error: 'Bank2' was not declared in this scope
Cleverblocks_test_2:259: error: expected primary-expression before ']' token
Cleverblocks_test_2:261: error: expected primary-expression before ']' token
Cleverblocks_test_2:261: error: 'Bank3' was not declared in this scope
Cleverblocks_test_2:261: error: expected primary-expression before ']' token
Cleverblocks_test_2:263: error: expected primary-expression before ']' token
Cleverblocks_test_2:263: error: 'Bank4' was not declared in this scope
Cleverblocks_test_2:263: error: expected primary-expression before ']' token
Cleverblocks_test_2.cpp: In function 'int GetValues(int*, int)':
Cleverblocks_test_2:294: error: expected primary-expression before ']' token[/font]

Code: [Select]
void CheckBlocks(int Block_1[], int Block_2[], int Block_3[], int Block_4[]){
  int quadrent = 0;
  Block_1[] = GetValues(Bank1[], quadrent);
  quadrent = 1;
  Block_2[] = GetValues(Bank2[], quadrent);
  quadrent = 2;
  Block_3[] = GetValues(Bank3[], quadrent);
  quadrent = 3;
  Block_4[] = GetValues(Bank4[], quadrent);
}//end function

 


int GetValues(int Bank[], int quad){
    if (quadrent == 0){
      for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       int Analog_voltage = analogRead(A15);
       }//end for loop
       }// end if
    else if (quadrent == 1){
      for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       int Analog_voltage = analogRead(A14);
       }//end for loop
       }// end if
    else if (quadrent == 2){
      for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       int Analog_voltage = analogRead(A13);
       }//end for loop
       }// end if
    else {
      for(int channel= 0; channel < 16; channel++){
       set_channel(channel); // turns on channel
       int Analog_voltage = analogRead(A12);
       }//end for loop
       }// end if
       return Bank[];
}// end function

//timer1 interrupt 1Hz
ISR(TIMER5_COMPA_vect){
      for(int quadrent = 0; quadrent < 4; quadrent++){
       for(int channel= 0; channel < 16; channel++){
         
     }//second for loop
    }// first for loop 
}//Interrupt

//This function activates the muxes
void set_channel(int channel) {
     switch(channel)
       {
        case 0:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 1:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 2:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 3:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 4:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 5:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 6:  digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
        case 7:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, LOW);
                 break;
       case 8:   digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 9:  digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 10: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 11: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, HIGH); 
                 break;
        case 12: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 13: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 14: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;
        case 15: digitalWrite(S0_pin, HIGH);
                 digitalWrite(S1_pin, HIGH);
                 digitalWrite(S2_pin, HIGH);
                 digitalWrite(S3_pin, HIGH);
                 break;       
        default: digitalWrite(S0_pin, LOW);
                 digitalWrite(S1_pin, LOW);
                 digitalWrite(S2_pin, LOW);
                 digitalWrite(S3_pin, LOW);   
                 break;
                 }//end case
}//end function



Thank you :)

michinyon

Well your syntax isn't C or C++.   I don't know what you are trying to do.

PaulS

Code: [Select]
void CheckBlocks(int Block_1[], int Block_2[], int Block_3[], int Block_4[]){
How is this function supposed to know how big the arrays are?

If this function is supposed to search the array to find the last valid value, you should be passing pointers to the function, instead.

The "expected primary-expression before ']' token" means that the compiler expected to find an array size after the [, before the ]. You didn't supply the size.

Code: [Select]
  Block_1[] = GetValues(Bank1[], quadrent);
You can't assign an entire array this way. You need to pass the array to be populated, along with the size, to the GetValues() method. If the GetValues() method is properly defined, then it knows that the first argument is an array. You don't need the [] in the call.


psppb

Thank you.

I understand now that I am suppost to give the compiler the size of the array via pointer. How do I output or return an array from a function?

Thanks

Bill

pYro_65

You can't return an actual array, however you can return a pointer to an array defined in appropriate memory ( returning local array pointer = bad ). The pointers don't carry size info either. char, char*, char[5], char[10] are all different types, the compiler uses its smarts to deduce which types are compatible at compile time ( it knows what size you declared your arrays to ).

You could pass in a pointer and length to the function.
void func( type *var, int len );

Or return a temporary object containing an array ( which gets copied ).
Code: [Select]
struct mytype{
  char arr[64];
};

mytype func( void ){
  mytype _return;
   //populate array.
  _return.arr[ 0 ] = 'a';
  return _return; //Client can inspect or copy object.
}


The copy can be avoided, but that's a whole 'nudder' story.




Go Up