Home » Programming Languages » C Programs » Passing Array Elements to Functions in C

Passing Array Elements to Functions in C


// #define CALL_BY_VALUE
#define CALL_BY_REFERENCE

#ifdef VALUE
/* Call by Value */
#include <stdio.h>

void function (int y);

int main(void) {
	int i, x[2] = {4, 10};

	for (i=0; i<2; i++)
		function(x[i]);

	return 0;
}

void function (int y) {
	printf("%d\n", y);
}
#endif

/* Call by Reference */
#ifdef CALL_BY_REFERENCE
#include <stdio.h>

void function (int *y);

int main(void) {
	int i, x[2] = {4, 10};

	for (i=0; i<2; i++)
		function(&x[i]);

	return 0;
}

void function (int *y) {
	printf("%d\n", *y);
}
#endif

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment