Passing arrays to functions

In the code below, it has two functions called “changeIntArray()” and “changeLongArray()” which changes the contents of each array (int or long). Is it possible to write one function the could change both ints and longs? The commented out area is what I tried and it didn’t work.

#include <stdio.h>

void changeIntArray(int ary[], int size) {
	int i;
	for(i=0; i<size; i++) {
		ary[i] = 0;
	}
}

void changeLongArray(long ary[], int size) {
	int i;
	for(i=0; i<size; i++) {
		ary[i] = 0;
	}
}

/**************************************
void changeAnyArray(void ary[], int size) {
	int i;
	for(i=0; i<size; i++) {
		ary[i] = 0;
	}
}
/**************************************/
int main() {
	int intAry[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	long longAry[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int i;

	for(i=0; i<sizeof(intAry)/sizeof(int); i++) {
		printf("intAry[%d]=%d \n", i, intAry[i]);
	}
	printf("\n\n");
	changeIntArray(intAry, sizeof(intAry)/sizeof(int));
	for(i=0; i<sizeof(intAry)/sizeof(int); i++) {
		printf("intAry[%d]=%d \n", i, intAry[i]);
	}

	printf("\n\n\n\n");

	for(i=0; i<sizeof(longAry)/sizeof(int); i++) {
		printf("longAry[%d]=%d \n", i, longAry[i]);
	}
	printf("\n\n");
	changeLongArray(longAry, sizeof(longAry)/sizeof(long));
	for(i=0; i<sizeof(longAry)/sizeof(long); i++) {
		printf("longAry[%d]=%d \n", i, longAry[i]);
	}

	printf("\n\n\n\n");

	/**************************************
	for(i=0; i<sizeof(longAry)/sizeof(long); i++) {
		printf("longAry[%d]=%d \n", i, longAry[i]);
	}
	printf("\n\n");
	changeAnyArray((long)longAry, sizeof(longAry)/sizeof(long));
	for(i=0; i<sizeof(longAry)/sizeof(long); i++) {
		printf("longAry[%d]=%d \n", i, longAry[i]);
	}
	/**************************************/
	return 0;
}

C isn’t run-time typed, so you’d either have to use C++ inheritance to dispatch (OK for statically typed call sites),
or pass the information required as another parameter and use switch - case.

It's possible but it would be messy. You would need to pass a void *ptr to the first element and then cast that depending on the element size of the array. Unless you are going to do all the byte-to-longer-type indexing yourself, you have to tell the compiler how big each element is. If you are going to do it yourself, you have to know whether the Arduino is big or little endian.

MarkT:
or pass the information required as another parameter and use switch - case.

It doesn't sound like you would gain much from having one function with a switch statement VS two functions - one for ints and one for longs. ...but anyway, I guess you are saying pass another variable to the function to tell the switch statement to work on ints or longs - right?

You can use templates. Similar to what I poseted in liudr’s thread.

template< typename T, unsigned N >
  void changeArray( T (&ary)[ N ] ){

    for( int i = 0 ; i < N ; ary[ i++ ] = 0 );
  }

I agree with Sparkfuntoday. I taught in the Computer Science department of a Big 10 university and have seen thousands of students try to write “Swiss Army Knife” functions. That is, they try to make a function do multiple things by passing “flag” parameters. This is rarely a good idea. The concept of cohesion in programming suggests that, if you can’t verbally state what a function does in two sentences or less, it probably isn’t well-designed and probably needs to be broken down. My own personal coding rule is: One function, one task. It just make debugging and code maintenance much easier.