# Function, struct or class?

I am trying to learn more about arduino programming, and recently i've written functions for randomizing and then modifying a single array. Now i'd like to do the same for four individual arrays. This is where i'm getting stuck. My questions:

Am i right to assume that it's not possible calling a function for multiple individual arrays?

Perhaps a separate question, but if a class or struct would be more suitable for multiple instances, what are the major differences worth considering to choose the right approach?

• Use arrays. You can pass arrays to functions. On what is more suitable, it depends on what you want to do. In real life, arrays rarely exists by themselves. You can use a struct to group an array and its related data, and a class if you want to tie array, related data and functions in one package.
– user31481
Jan 3, 2018 at 11:28
• Thanks for your clarification! Just to make sure we're on the same page: by individual arrays i mean that the four arrays all have different elements (If i'm not mistaken this implies individually declared arrays, no?). I've tried to read up on this, but i can't seem to come up with a solution where the same function returns four separately declared arrays, each with different elements. I'd be happy for a suggestion that points me in the right direction!
– Erik
Jan 3, 2018 at 14:37

Are you trying to use the same function for different arrays at different times, or are you trying to use a function that handles four different arrays at the same time?

If the former, you'd write something like

void myfunc( T *arr, size_t size ) // where T is the type of the array
{
for ( size_t i = 0; i < size; i++ )
arr[i] = some_new_value();
}


then call it for each array:

T arr1[N];  // where T is int, float, double, whatever
T arr2[M];
T arr3[O];
T arr4[P];

myFunc( arr1, N );
myFunc( arr2, M );
myFunc( arr3, O );
myFunc( arr4, P );


This assumes all the arrays have the same base type (e.g., they're all arrays of int).

If you want a function to work on four arrays simultaneously, you'd write it as

void myFunc( T *a1, size_t a1_size,
T *a2, size_t a2_size,
T *a3, size_t a3_size,
T *a4, size_t a4_size )
{
// code for handling each array
}


While I wrote it such that all the arrays have the same type, it doesn't have to be this way. Each array can be a different type (a1 can be int, a2 can be double, etc.).

Am i right to assume that it's not possible calling a function for multiple individual arrays?

What would give you that impression? An array is just a pointer to a block of memory - as such it's just a variable. You can pass as many as you like to a function.

Just remember that a function can't know how big the array is that you pass it, since it only gets the pointer to the start of the array, so you need to pass the size of the array with it. You can't rely on the sizeof operator, since that is a compile-time directive that gives the size of a variable, which inside the function will be the size of a pointer (2 bytes on an 8 bit system). It can only give the size of an array (and then only the memory size in bytes, not the count of elements) if its run in the same (or a subordinate) scope as the declaration of the array.

Perhaps a separate question, but if a class or struct would be more suitable for multiple instances, what are the major differences worth considering to choose the right approach?

A struct is just a way of grouping variables into compound types. Think of them as database records. Great for if you want to keep related, but different, information together in one block. Classes are the same thing but add functions as well.*

* That's a very simplistic description. In actuality you can have functions in structs and not have functions in classes. The real difference between them is how access controls (private/protected/public) are implemented (there is none in structs, and they are all there in classes) and how inheritance / polymorphism works.

• Regarding your first question: I can't figure out a way to pass four individually declared arrays to the same function, and figured i'd ask beforehand instead of trying something that in fact is impossible. If it's doable, please elaborate. Also, thanks for describing things in the latter part of your answer.
– Erik
Jan 3, 2018 at 16:36
• @Erik I can't unless you show us what it actually is you are trying to do. Jan 3, 2018 at 16:37
• "An array is just a pointer to a block of memory" - if we're talking about C or C++, this isn't correct. Array expressions will be converted ("decay") to pointer expressions in most contexts, but array objects are not pointers. An array is simply a contiguous sequence of elements - no storage is set aside for a pointer to the first element. Jan 3, 2018 at 18:52
• @Majenko: An array is not an address. An array is a sequence of elements. An array expression gets converted to a pointer expression as necessary, but that's not the same thing. Jan 3, 2018 at 19:58
• @Majenko: Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array. When you pass an array expression as an argument to a function (such as to printf), what the function receives is a pointer. But the array itself is not a pointer. Jan 3, 2018 at 21:00