0

After seeing this question Swapping in a char * array[ ] leading to issues

User Claudiu gave a solution to swap the position of a string in an array

 tmp = array[compare];
            array[compare] = array[index];
            array[index] = tmp;

However, i was wondering how to make that in a function by itself. using pointers, and I just can't understand the link between pointers and char arrays.

1 Answer 1

1

You can define a function:

void swapArrayItems(char* array[], int index1, int index2)
{
   char* tmp = array[index1];
   array[index1] = array[index2];
   array[index2] = tmp;
}

And then use it as:

swapArrayItems(array, compare, index);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much, just one more tiny thing, what is the difference between having char* array[] and char *array in the argument?
char* array[] usually means an array of strings. char* array usually means an array of characters representing one string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.