Go Down

Topic: Is there really no way to code this easier? (Read 176 times) previous topic - next topic

Alucard27

Heres a snippet out of my code:

Code: [Select]
void muster()
{
    int pack1[] = {10,20,30};
    pick(pack1,1);
    int pack2[] = {25,35,40};
    pick(pack2,1);
}

void pick(int numbers[],int faktor)
{


I cant do this:
Code: [Select]
pick( {10,20,30},1 )

I cant redefine arrays either, what should i do? this is horrible

CtrlAltElite

Quote
I cant do this:
Because this is C++.

Here's a snippet of my answer : What do you mea
Has there been an outbreak of the stupid virus, and I didn't get the memo?

econjack

If you've got that info, why not:

Code: [Select]

//pick( {10,20,30},1 )

   pick(10);



or

Code: [Select]

//pick( {10,20,30},1 )

   index = 1;
   pick(pack1[index]);


// more code...

void pick(int element)
{
// code
}

BulldogLowell

I cant redefine arrays either, what should i do? this is horrible
Code: [Select]
void setup() {
  Serial.begin(9600);
  int pack1[] = {10, 20, 30};
  pick(pack1, sizeof(pack1)/sizeof(pack1[0]));
}

void loop() {

}


void pick(int numbers[], int faktor) {
  for (int i = 0; i < faktor; i++)
  {
    Serial.println(numbers[i]);
  }
}

CtrlAltElite

Code: [Select]
void setup() {
  Serial.begin(9600);
  int pack1[] = {10, 20, 30};
  pick(pack1, sizeof(pack1)/sizeof(pack1[0]));
}

void loop() {

}


void pick(int numbers[], int faktor) {
  for (int i = 0; i < faktor; i++)
  {
    Serial.println(numbers[i]);
  }
}


But that looks nothing like the OP.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

Alucard27

Thanks guys, i stumbled over const and now i've changed the code to this:

Code: [Select]
void muster()
{

  pick((const int[]){11,21,31}, 1);
  pick((const int[]){18,28,38}, 1);
  pick((const int[]){11,12,13}, 1);
}

void pick(const int c_numbers[],int faktor)
{
    int numbers[pick_size];
    for (int i=0; i<pick_size; i++)
    {
        numbers[i] = c_numbers[i]; 
    }

//Do something with numbers[i]


It works as intended, still thanks!

232

So it works (?) but...
Just for drill, or future reference,look up"passing
An Array to function".
Thunder is good, thunder is impressive; but it is lightning that does the work.

Go Up