I would like to make a function that can take a variable number of parameters at once to do something like this:
void function(char *args[]){ ... }
int main(){
function("a","b","c");
}
You are looking for Variadic functions.
Check this example:
#include <stdio.h>
#include <stdarg.h>
double average(int num, ...) {
va_list valist;
double sum = 0.0;
int i;
/* initialize valist for num number of arguments */
va_start(valist, num);
/* access all the arguments assigned to valist */
for (i = 0; i < num; i++) {
sum += va_arg(valist, int);
}
/* clean memory reserved for valist */
va_end(valist);
return sum/num;
}
int main(void) {
printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2, 3, 4, 5));
printf("Average of 5, 10, 15 = %f\n", average(3, 5, 10, 15));
}
Output:
Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000
Read more in Variadic Functions in C and the Reference.
Tip: Think twice before using Variadic functions. As @user694733 commented, they have no compile time type safety or protection against invalid number of arguments. They can usually be avoided with better program design.