看到这个问题后Swapping in a char * array[ ] leading to issues
用户Claudiu给出了交换数组中字符串位置的解决方案
tmp = array[compare];
array[compare] = array[index];
array[index] = tmp;
但是,我想知道如何在函数中单独实现它。使用指针,但我无法理解指针和字符数组之间的联系。
最佳答案
您可以定义一个函数:
void swapArrayItems(char* array[], int index1, int index2)
{
char* tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
然后将其用作:
swapArrayItems(array, compare, index);
关于c - 用指针交换数组索引函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225013/