My question is to understand why this is happening.
My goal is to pass the name “Joe” and the last name “Bloggs” into the swap function I created and enable it. (So, the first name will now contain Bloggs the other way around)
I created a swap function that takes pointers to first and last name as arguments. The function works because, using the print operator, I see that the values of the two lines have been 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 the problem is that after calling the swap function, I print the first and last name again, but they do not display 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
}
SO, , , . , , . C, .