Modifying array by using as argument to library function.

I want to have a library function perform some modification to an array and then allow that array to be used by other functions in the main program, as well as other library/main functions that are called.

From what I understand with C/C++, you cannot use an array as an argument, you specify the array as a pointer and have the function perform its work on the understanding that there is an array at that pointer for it to work on. I’ve looked at a few tutorials and haven’t had much success with the different variations that it suggests, I am also doing this via a library function which the examples I’ve found don’t address.

This is a simplified version of what I am working on:

INO:

#include <passedarray.h>

// create array to be passed to library function and modified
byte main_array[16];

void setup()
{  
  Serial.begin(19200);
  
  // populate main_array with characters
  for (int x=0; x < 16; x++){
    main_array[x] = 28;
  }
  
  // print out main_array to serial console
  Serial.print("main array:");
  for (int h=0; h < 16; h++){
    Serial.print(main_array[h], HEX);
  }
  Serial.print("\r\n");  
  
  // call library function with array pointer as argument, this function should modify the array
  PASSARRAY PASSARRAY(main_array);
  
  // print out modified array to serial console
  Serial.print("returned modified array:");
  for (int s=0; s < 16; s++){
    Serial.print(main_array[s], HEX);
  }
  Serial.print("\r\n");
}

void loop() {
}

passedarray.cpp:

#include "passedarray.h"
  
PASSARRAY::PASSARRAY(byte passed_array)
{
  printArray();
  modifyArray();
  // Maybe i need to return the array in some way? The arrays are different at the end, probably due to this function creating a copy of the array?
}

void PASSARRAY::modifyArray() // Populate passed_array with characters
{
  Serial.print("library modified array:");
  for (int x=0; x < 16; x++){
    passed_array[x] = 27;
    Serial.print(passed_array[x], HEX);
  }
  Serial.print("\r\n");
}

void PASSARRAY::printArray() //print out passed_array to serial console
{
  Serial.print("passed array:");
  for (int a=0; a < 16; a++){
    Serial.print(passed_array[a], HEX);
  }
  Serial.print("\r\n");
}

passedarray.h:

#include <Arduino.h>
  
#ifndef PASSARRAY_h
#define PASSARRAY_h

class PASSARRAY
{
  public:
    byte passed_array[16];
    PASSARRAY(byte passed_array); //This should at least be a copy of the array sent by the caller?

  private:

    void modifyArray();
    void printArray();
};

#endif

The output I get from this is:

main array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C
passed array:3D4AC875347038FBC7A7BFA6BE3FE63F
library modified array:1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B
returned modified array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C

I am expecting:

main array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C
passed array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C // The same array that was passed to the function
library modified array:1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B
returned modified array:1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B // The array has been modified by the function and is accessible from main

Does anyone have any insight on where I am going wrong?

try:

PASSARRAY::PASSARRAY(byte passed_array[])

FYI, when passing array to a function, it's important to pass along the size of it. Because you can't figure out the size in the function

This results in the following compile error:

/home/spoonman/Arduino/libraries/arraypasstest/passedarray.cpp:3:1: error: prototype for 'PASSARRAY::PASSARRAY(byte*)' does not match any in class 'PASSARRAY'
 PASSARRAY::PASSARRAY(byte passed_array[])
 ^
In file included from /home/spoonman/Arduino/libraries/arraypasstest/passedarray.cpp:1:0:
/home/spoonman/Arduino/libraries/arraypasstest/passedarray.h:6:7: error: candidates are: constexpr PASSARRAY::PASSARRAY(PASSARRAY&&)
 class PASSARRAY 
       ^
/home/spoonman/Arduino/libraries/arraypasstest/passedarray.h:6:7: error:                 constexpr PASSARRAY::PASSARRAY(const PASSARRAY&)
/home/spoonman/Arduino/libraries/arraypasstest/passedarray.h:10:5: error:                 PASSARRAY::PASSARRAY(byte)
     PASSARRAY(byte passed_array); //This should at least be a copy of the array sent by the caller?
     ^
Error compiling.

I've seen examples that use the array length as an argument, I plan on implementing that as the next step after solving this issue.

You need to also change the parameter in the .h file:

PASSARRAY(byte passed_array[]);
class PASSARRAY
{
  public:
    byte passed_array[16];

Is the class supposed to make a copy of the array? If not, what is this for?

Using names of member variables as function argument names is NOT a good idea.

arduino_new: You need to also change the parameter in the .h file:

PASSARRAY(byte passed_array[]);

Ah, looks like I didn't save the header file properly before compiling. I get the following result:

main array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C
passed array:3CEAC87534703A7BC7A7BFAEBE3FE63F
library modified array:1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B
returned modified array:1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C

PaulS: class PASSARRAY {  public:    byte passed_array[16];

Is the class supposed to make a copy of the array? If not, what is this for?

Using names of member variables as function argument names is NOT a good idea.

I assume that this shouldn't be in there, but I can't find a way to get it to compile without declaring the array. If I comment it out I get this:

error: 'passed_array' was not declared in this scope
     Serial.print(passed_array[a], HEX);

error: 'passed_array' was not declared in this scope
     passed_array[x] = 27;

Do you have any suggestions on how to appropriately fix this?

To make it easier for us to keep track of what you’re doing, you should always post you complete, updated code every time you change something. To make it even easier for forum members, combine and post your entire code as a single .ino file in order: contents of .h file, contents of .cpp file, setup(), loop(). That way we can copy one file, paste in IDE, and compile. You can always break it apart again later. Also, of course, remove the #include of the .h file.

gfvalvo:
To make it easier for us to keep track of what you’re doing, you should always post you complete, updated code every time you change something. To make it even easier for forum members, combine and post your entire code as a single .ino file in order: contents of .h file, contents of .cpp file, setup(), loop(). That way we can copy one file, paste in IDE, and compile. You can always break it apart again later. Also, of course, remove the #include of the .h file.

Here is the combined code:

// ************ passed_array.h ***************

#include <Arduino.h>
  
#ifndef PASSARRAY_h
#define PASSARRAY_h

class PASSARRAY
{
  public:
    byte passed_array[16]; // It was suggested by PaulS that variable names and function arguments should not be the same, i'm not sure what the best way to correct this is?
    PASSARRAY(byte passed_array[]); //This should at least be a copy of the array sent by the caller?

  private:

    void modifyArray();
    void printArray();
};

#endif

// ************ passed_array.cpp ***************

PASSARRAY::PASSARRAY(byte passed_array[])
{
  printArray();
  modifyArray();
  // Maybe i need to return the array in some way? The arrays are different at the end, probably due to this function creating a copy of the array?
}

void PASSARRAY::modifyArray() // Populate passed_array with characters
{
  Serial.print("library modified array:");
  for (int x=0; x < 16; x++){
    passed_array[x] = 27;
    Serial.print(passed_array[x], HEX);
  }
  Serial.print("\r\n");
}

void PASSARRAY::printArray() //print out passed_array to serial console
{
  Serial.print("passed array:");
  for (int a=0; a < 16; a++){
    Serial.print(passed_array[a], HEX);
  }
  Serial.print("\r\n");
}

// ************ main.ino ***************

// create array to be passed to library function and modified
byte main_array[16];

void setup()
{  
  Serial.begin(19200);
  
  // populate main_array with characters
  for (int x=0; x < 16; x++){
    main_array[x] = 28;
  }
  
  // print out main_array to serial console
  Serial.print("main array:");
  for (int h=0; h < 16; h++){
    Serial.print(main_array[h], HEX);
  }
  Serial.print("\r\n");  
  
  // call library function with array pointer as argument, this function should modify the array
  PASSARRAY PASSARRAY(main_array);
  
  // print out modified array to serial console
  Serial.print("returned modified array:");
  for (int s=0; s < 16; s++){
    Serial.print(main_array[s], HEX);
  }
  Serial.print("\r\n");
}

void loop() {
}

In your modifyArray() and printArray() functions, you are operating on the array member of your class instead of the one you are passing.

To fix this, you should either, 1) copy the data from the array you passed to the class to the member array in your class before calling modifyArray() and printArray().

or

2) pass the array you just received to both the modifyArray() and printArray() just like how you pass it to the constructor.

It's not really clear what you're trying to do. You have an instance member array that you don't use. You have instance functions that can't be called from the main program because they're private and only called from the constructor. And, you pass an argument to the constructor that you don't use.

You have an instance member array that you don't use. You have instance functions that can't be called from the main program because they're private and only called from the constructor. And, you pass an argument to the constructor that you don't use.

Aside from that, you got everything right. Except that "aside from that" is very, very little.

arduino_new: In your modifyArray() and printArray() functions, you are operating on the array member of your class instead of the one you are passing.

To fix this, you should either, 1) copy the data from the array you passed to the class to the member array in your class before calling modifyArray() and printArray().

or

2) pass the array you just received to both the modifyArray() and printArray() just like how you pass it to the constructor.

Thanks, I have it working now.