Swapping first and last name in C

  • A+
Category:Languages

My question is to understand why this happens.

My goal is to pass a first name 'Joe' and last name 'Bloggs' into the swap function I created and have it switch them around. (So First name will now contain Bloggs vice versa)

I've created a swap function that takes pointers to the first and last name as arguments. The function appears to work because using the print statement I can see the values of the two strings has changed.

#include <stdio.h>  void swap(char * first, char * last) {     char * temp;     temp = first;     first = last;     last = temp;     printf("at end of swap func the first name is: %s/n", first);     printf("at end of swap func the last name is: %s/n", last); } 

Now here appears to be the problem, after the swap function is called, I print the first and last name again, but they aren't displaying the results of the swap function.

int main() {     char * first = "Joe";     char * last = "Bloggs";     printf("The original format: %s %s/n", first, last);     swap(first, last);     printf("The new format: %s %s", first, last);//new format was supposed to be Bloggs Joe } 

I have searched SO but the questions I've found are not specific to mine. As I said earlier, I'd really like to know why this happens. I have very little knowledge of C so any suggestions will be appreciated.


In C, all parameters are pass by value. That means that first and last in the function are distinct from first and last in main. Changes to a local variable are not reflected in the caller.

You need to pass the address of the variables you want to change. Then in the function, you dereference the given pointers to change what they point to.

So change your function as follows:

void swap(char ** first, char ** last) {   char * temp;   temp = *first;   *first = *last;   *last = temp;   printf("at end of swap func the first name is: %s/n", *first);   printf("at end of swap func the last name is: %s/n", *last); } 

And call it like this:

swap(&first, &last); 

Note that what's actually being swapped are the pointer values, not what the pointers actually point to. You could swap the characters between the two strings, but the strings in question are string literals which cannot be modified.

If you had char arrays that contained strings, then you could swap the characters. In fact, you would have to do it that way because arrays are not the same as pointers.

Here's a full example of how you would do a full string swap:

#include <stdio.h> #include <string.h>  // assumes that each array is big enough to hold the contents of the other void swap(char *first, char *last) {   int len1 = strlen(first);   int len2 = strlen(last);   int maxlen = (len1 > len2) ? len1 : len2;   char temp[maxlen+1];   strcpy(temp, first);   strcpy(first, last);   strcpy(last, temp);    printf("at end of swap func the first name is: %s/n", first);   printf("at end of swap func the last name is: %s/n", last); }  int main() {   char first[10] = "Joe";   char last[10] = "Bloggs";   printf("The original format: %s %s/n", first, last);   swap(first, last);   printf("The new format: %s %s", first, last); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: