Last month’s Exercise was rather silly: Rarely do you need to swap elements between two integer arrays. As Chris pointed out in the Exercise’s comments, “Would it not be easier to just use the array which contains the values you need?” Indeed, it is, which leads up to this month’s Exercise.
Consider that you have two arrays:
char *beatles[4] = { "John", "Paul", "George", "Ringo" }; char *stones[4] = { "Mick", "Keith", "Ronnie", "Charlie" };
These are not string arrays, but pointer arrays. The declaration char *
defines a pointer, and char *beatles[4]
references four pointers. The pointers are all preset (in both arrays above) to various strings.
Indeed, when you see such a string declaration, you must be careful not to manipulate the strings directly. If you do, you experience all sorts of pointer woe, but that’s not the point of this Exercise.
Your task is to swap pointers between the two arrays. I would write, “swap strings between the two arrays” (and that’s this post’s title), but what you’re really doing is swapping pointers. In fact, if your solution involves manipulating the strings, you’ll soon discover the ugly truth — but I’ll leave that frustration to you.
Here’s the rest of the main() function for this month’s Exercise:
int main() { char *beatles[4] = { "John", "Paul", "George", "Ringo" }; char *stones[4] = { "Mick", "Keith", "Ronnie", "Charlie" }; printf("Original arrays:\n"); show_arrays(beatles,stones); swap_arrays(beatles,stones); printf("After swap:\n"); show_arrays(beatles,stones); return(0); }
The solution is quite similar to last month’s Exercise; you write your own versions of the show_arrays() and swap_arrays() functions.
I once met Ronnie Wood. His father in law was a friend of my father.
A couple of weeks ago I posted a bit of code using XOR to swap integers. I don’t see any reason it wouldn’t also work for pointers.
Pretty cool about Ronnie Wood.
To me, it would be scary to do the swap with the XOR process. Logically, it should work. You try it first! 😀
Sadly I have never met a Beatle or a Queen :(. I have seen Freddie Mercury’s house in West London though, and of course Abbey Road Studios. Ringo has just been given a Knighthood, 20 years after Sir Paul. (Sorry for drifting off topic!)
I have added XOR swapping of pointers to my long list of things to try. Worthwhile to at least try as swapping pointers is a valuable sorting technique if you are sorting something large.
For the purposes of the above exercise can we assume the array size is #defined or hard-coded?
I hard coded the 4 into the arrays and loops, though normally I would #define it instead. Too many times I’ve had to trudge through my code to look for things like that, which is why I typically hard code those things.
Just to let you know XOR doesn’t work with pointers – I get the following compiler error:
invalid operands to binary ^
I don’t see why it isn’t possible in principle – after all a pointer is just an integer – but gcc at least isn’t coded to allow it. I suppose I could take it further and implement something to XOR pointers but I can’t really be bothered!
I tried it as well, even casting the pointers as unsigned integers. Didn’t work. I suppose C may be smarter than I think!