# Question on Functions and arrays

I’m running into situations where it would be useful to go beyond what I understand about functions

I have multiple functions that are identical except they each use data from different arrays,

Is there a way to reverse this? Ie

function_name(val, val, identifier_of_array);

Instead of what I have now, multiple variations of;

function_name_for_array_a(val, val);

Pass by Reference sounds promising.

What actions are taken on the arrays?

I’ve encountered my dilemma on various parts(functions) in my program, to generalise
I’m taking two unit8_t[] arrays, chopping them up and reassembling them as a new array or printing the result.
As my program grows the combinations have got out of hand.

Ok, pass by reference took me towards this, which I think is ok.

uint8_t Array[];
uint8_t val;

void func(theray[],val){
~the meat~;
}

func(Array, val);

I thought I had tried that but obviously not.
Thank you.

uint8_t Array[];

That is an array with zero elements; it will result in undefined behaviour of your application at the moment that you assign a value to e.g. Array[0].

I'm not sure if you know the size of the array. It needs to be fixed, e.g.

``````// array with 20 elements
uint8_t Array[20];
``````

Note that if the array holds text (e.g. "Hello"), the above can hold up to 19 characters to leave space for the terminating nul character.

Mattty5:
Ok, pass by reference took me towards this, which I think is ok.

uint8_t Array[];
uint8_t val;

void func(theray[],val){
~the meat~;
}

func(Array, val);

that works because it's referring to an array of integers without specifying the # of elements. looks like a synonym for a pointer

the more common approach is

``````void func(int *buf, int size){
}
``````

gcjr:
the more common approach is

``````void func(int *buf, int size){
``````

}

I beg to differ. The more common approach would be

``````void func(uint8_t* array, size_t size) {
}
``````

yes type
but the point is "uint8_t arr []" vs "uint8_t *arr"

sometimes a message packet is defined

``````struct TlvPkt_s {
int  type;
int  length;
int  data [];
};
``````

to provide a pointer to the data in a packet of unknown length

Thank you all very much!
I created some extra confusion by not showing that I do define my array sizes, some are of fixed length, some have a maximum size, I pass the actual length in both cases.
I was trying to simplify my problem and went too far, sorry!

As I press on I find I have one more related question...

How might I implement a similar approach with struct as mentioned above?

Ie

void function(struct_id){
// all variables inside this function read or write to //struct_id.data_id
}

Think I’ve got it,

``````struct Data {
float a;
uint8_t b;
uint8_t c[12];
} ;

struct Data V;
struct Data T;

void function(Data &Vals){
//do stuff to Vals.a //b //c
}

//
function(V);
function(T);
//
``````

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