3

The problem is that this code won't interchange these 2 strings. I'm new to programming but I can tell that the problem is that swap function, but I do not know how to fix it.

I tried to add strcpy instead of "=" in swap but that didn't worked.

#include <stdio.h>
#include <stdlib.h>

void swap(char *t1, char *t2) {
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}
int main() {
    char *s[2] = {"Hello", "World"};
    swap(s[0], s[1]);
    printf("%s\n%s", s[0], s[1]);
    return 0;
}
1
  • 1
    Don't forget that in C all function arguments are passed by value, which means their values are copied. When you change a copy the original won't be changed. Do some research about emulating pass by reference in C. Commented Feb 5, 2019 at 12:34

3 Answers 3

6

You want to use out parameters here, and since your strings are represented as pointers, you need pointers to pointers:

void swap(char **t1, char **t2) {
    char *t;
    t = *t1;
    *t1 = *t2;
    *t2 = t;
}

Call it like this:

swap(&s[0], &s[1]);

I tried to add strcpy instead of "=" in swap but that didn't worked.

The reason why that doesn't work is because the strings are actually stored in the program's binary and therefore can't be modified, and with strcpy you would write over them. If you copy them to the stack or the heap instead then you can do the swap with strcpy. Of course that's going to be less efficient than just swapping the pointers, but this is how it would look like:

void swap(char *t1, char *t2) {
    char buf[16]; // needs to be big enough to fit the string
    strcpy(buf, t1);
    strcpy(t1, t2);
    strcpy(t2, buf);
}

Also you would need to change the definition of s to something akin to

char s[2][16] = { "Hello", "World" }; // strings are copied to the stack now
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your response!
2

Check the types carefully.

What you have got as array members are pointers (to the starting element of string literals). You need to swap the members in a way so that they point to the other string literal. So, you need to change those pointers themselves.

So, you need to pass pointer to those pointers and then make the change from the called function.

Do something like

swap(&(s[0]), &(s[1]));

and then, in the called function:

void ptrSwap(char **t1, char **t2) {
    char *temp;
    temp=*t1;
    *t1=*t2;
    *t2=temp;
}

Bonus points: Name your functions (and variables, too, wherever applicable) meaningfully.

1 Comment

Thank you very much for your response!
2

You need to passing the pointer of pointer, i.e. address of the position in array where the strings are present, so that you can swap and place correct addresses there.

Try the below code:

#include <stdio.h>
#include <stdlib.h>

void swap(char **t1, char **t2) {
    char *t;
    t=*t1;
    *t1=*t2;
    *t2=t;
}
int main() {
    char *s[2] = {"Hello", "World"};
    swap(&s[0], &s[1]);
    printf("%s\n%s", s[0], s[1]);
    return 0;
}

Output:

World
Hello

1 Comment

Thank you very much for your response!

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.