C - Function with variable number of arguments and command line arguments
问题 I need to sort n number of strings lexicographically that are arguments of a function with variable number of arguments. In main function, strings are read as command line arguments. Here is my code: #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> void sort(int n,...) { //store optional arguments (strings) to array arr va_list args; va_start(args,n); char **arr=malloc(n * sizeof(char*)); int i; for(i=0;i<n;i++) arr[i]=malloc((strlen(va_arg(args,char*)) + 1) *