I am writing a program that takes in a string array and sorts it then uses searching functions to search for a target. The string array I have set up works for all my sorting functions but I need the data to be in a pointer string array in order to make use of strcmp in my searching functions. I've attached the most recent attempt in my main function. The rest of the main function includes the sorting and searching function but what I am posting only contains the initialization of the arrays and printing them out. In the actual program, it will be copying the contents from the sorted array after whichever sorting function the user chooses completes. I just want to find out how I would copy the contents.
int main(void) {
char arr[][MAX_LEN] = {"AAAAA", "a", "AAA", "aaaa", "AAa", "AAAAa", "AAa", "A", "aa", "Aaa"}; // initializes string array
char *target[] = {"A"}; //initializes target
int n = sizeof(arr)/sizeof(arr[0]); // initializes n
char *arry = (char* )malloc(n); // initizlizes *arry
int i;
printf("Given string array is:");
for (i = 0; i < n; i++)
printf(" %s ", arr[i]); // prints out arr
for (i = 0; i < n; i++) { // this was my first attempt to copy arr into *arr.
strcpy(arry[i], arr[i]);
printf(" %s ", arry[i]); // used for testing to see if copying worked.
}
}
char *arr[] = {...but note that the strings will now be lieals and read-only. You can copy them, but cannot change the original.strcmp(arr[0], "some string")? Orstrcmp(arr[0], target[0])? I don't see why you would want to copy the array to "another array" in order to usestrcmp. Seems an XY problem to me