Array passing problem


this is a test code. i have tried to send data to another function. but the results were like this figure. can anyone help me to fix it

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

1 Like

Apart from any other problem you are passing k[3] to the function but the k array does not have a level 3, only 0, 1 and 2

1 Like

probably, but not until you explain what is the expected result

1 Like

We want more than snippets of code. See here. Post the whole code.

We don't like pictures of code. We can't copy the code into a text editor or the IDE for examination or to try to run it. Post the in code tags, make it easy on your potential helpers.

1 Like

Pass the array by reference, and use a range-based for loop to prevent out of bounds errors:

void StateChanger(char (&a)[8], int (&b)[3]) {
  // you might want to use more meaningful variable names instead of a and b
  for (int &element : b) { // for each element in the array, by reference
    Serial.println(element);
    delay(500); // ?
  }
}

Then pass the array without square brackets:

StateChanger(state, k);

By using StateChanger(state1[8], k[3]), you're passing integers to a function that expects pointers, and you're accessing the arrays out of bounds, the compiler should have warned you about both mistakes, if it didn't, please open the IDE preferences and enable all warnings.

1 Like

Pick one:


int k[3] = { 6, 8, 9};

void setup() 
{
  Serial.begin(115200);

  function1( k);
  function2( k);
  function3( k);
  function4( k);
  function1( &k[0]);
  function2( &k[0]);
  function3( &k[0]);
//  function4( &k[0]); // not possible, "by reference" should use the array
}

void loop() 
{
}

void function1( int *a)
{
  for( int i=0; i<3; i++)
  {
    Serial.print( a[i]);
    Serial.print( ", ");
  }
  Serial.print( " sizeof=");
  Serial.print( sizeof( a));
  Serial.println();
}

void function2( int a[3])
{
  for( int i=0; i<3; i++)
  {
    Serial.print( a[i]);
    Serial.print( ", ");
  }
  Serial.print( " sizeof=");
  Serial.print( sizeof( a));
  Serial.println();
}

void function3( int a[])
{
  for( int i=0; i<3; i++)
  {
    Serial.print( a[i]);
    Serial.print( ", ");
  }
  Serial.print( " sizeof=");
  Serial.print( sizeof( a));
  Serial.println();
}

void function4( int (&a)[3])
{
  for( auto x:a)
  {
    Serial.print( x);
    Serial.print( ", ");
  }
  Serial.print( " sizeof=");
  Serial.print( sizeof( a));
  Serial.println();
}

The sketch in Wokwi:

1 Like

It should be noted that this approach is a bit misleading: it is exactly equivalent to void function1(int *) and void function3(int []), the compiler will not emit any warnings when you call it with an array that's too short, or if you go out of bounds inside of the function. It also doesn't pass any size information to the function (as demonstrated by Koepel's print(sizeof)).

For these reasons, I prefer passing by reference, the compiler will check the bounds, and inside of the function you know the exact size of the array that was passed in.
(There are cases where you have to pass a pointer + size, or preferably an std::span, e.g. if you want to be able to pass a subarray or an array with a dynamic size.)

2 Likes

The compiler should also be warning you that all of your char arrays are too short, there needs to be room for the terminating null.

1 Like

sorry. this is the full code. im testing pass array to a function method...

void StateChanger (char a[x] , int b[]);

void setup() {
Serial.begin(9600);
char state1[8]= "HUHUHUHU";
char state2[8]= "RRRGRGRR";
char state3[8]= "RRRGRGRR";
char state4[8]= "RRRGRGRR";
char state5[8]= "RRRGRGRR";
char state6[8]= "RRRGRGRR";
char state7[8]= "RRRGRGRR";
char state8[8]= "RRRGRGRR";
int k[3] = {6,8,9};
for (int i=0; i<3;i++) {
Serial.println(k[i]);
delay(500);}
StateChanger (state1[8] , k[3] );
}

void loop() {

}

void StateChanger (char a[8] , int b[3]){
for (int i=0; i<3;i++) {
Serial.println(b[i]);
delay(500);}
}

the result need to be same. i have send int array to the function and printed unexpected three values. 6 8 9 values are the values and after passing to the other function, i got -32126 , 18504 and 18504 instead of 6 8 9..

@ravirulz I think I mentioned all of them in my sketch. What you have is not one of those.
PieterP is right, the void function2( int a[3]) is a bit misleading and the "by reference" is very nice but new and not often used.

1 Like

Thank you very much sir. im new to arduino and u saved my day. im really grateful to u

thank you very much sir.

yes sir and thank you very much

This is because you passed 4th element of k instead of a pointer to k array. 4th element is non existent so you got gibberish

1 Like

See reply #3

1 Like

Thank you sir

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.