this is part of a function that takes input from terminal and sorts them depending on what the inputs are (sorting type, a version/variation of the sorting method to use, and the size of the array). This is what I have so far:
int main(int argc, char * argv[]) { //will have 3 arguments not including function name: sortingtype, version and arr size
int * arr = make_arr(argv[2], argv[3]); //[2]
if (strcmp(argv[1], "sortingtype1") == 0) {
SortingType1(arr, argv[3]); //[2][3]
}
else if (strcmp(argv[1], "sortingtype2") == 0) {
SortingType2(arr, argv[3]); //[2][3]
}
else {
return 0;
}
}
void test(){ //[1]
main("sortingtype1", "a", 10); //sortingtype, version and arr size
}
[1] I have a function test() to simulate input from terminal but I don't know if it works that way. I get an error saying that there are too many arguments to main.
[2] Whether or not I remove that testing function, I still get warnings about "passing argument (the arguments with argv[X]) makes integer from pointer without a cast".
[3] These also need to be type int and not type char*, how do I change them?
Any suggestions on how to go about this? I have seen solutions using sscanf, but would prefer a more basic solution around my skill level first for understanding.
Edit: segmentation faults from
int * SortingType2(int * arr, int len) {
for (int i=1; i < len; i++) {
int x = arr[i];
int j = i;
while ((j > 0) && (x < arr[j-1])) {
arr[j] = arr[j-1];
j--;
}
arr[j] = x;
}
return arr;
}
int main(int argc, char * argv[]) {
int size;
if (argc > 3) size = atoi(argv[3]);
int * arr = make_arr(argv[2][0], size);
if (strcmp(argv[1], "sortingtype1") == 0) {
SortingType1(arr, size);
}
else if (strcmp(argv[1], "sortingtype2") == 0) {
SortingType2(arr, size);
}
else {
return 0;
}
}