So first I created a struct of an array of variable size
typedef struct arr_string {
int size;
char* *arr;
} arr_string;
arr_string alloc_arr_integer(int len) {
arr_string a = {len, len > 0 ? malloc(sizeof(char*) * len) : NULL};
return a;
}
Then I wrote a function that is meant to create a new array (called c) of strings with all the possible combinations of the strings in a given array (called a).
int alternatingSort(arr_string a) {
int n = a.size;
arr_string c = alloc_arr_integer(100);
int count = 0;
//initialize array with empty values
for (int l = 0; l < c.size; l++){
c.arr[l] = "";
}
//fill array c with all possible combinations of values in C
for (int i = 0; i < n; i ++){
for (int m = i+1; m < n; m ++){
printf("hi1\n");
char* new = strcat(a.arr[m], a.arr[i]);
c.arr[count] = new;
count ++;
printf("hi2\n");
}
}
}
I get a bus error while running this and I'm not sure why. The first print statement "hi1" prints but not "hi2" so I believe the error is when I try to assign "new" to c.arr[count]. Did I make a mistake when allocating the memory for array c?