1

I am trying to swap 2 entries in an array of strings, but my swap function doesn't swap when called.

swap(char*, char*);

int main() {
    char *ptsr[2] = { "x", "y" };
    swap(ptsr[0], ptsr[1]);
}

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

Can someone identify and explain my mistake?

2
  • 2
    If you did that with int, what do you think swap(1,2); would do? Commented Jan 26, 2021 at 7:53
  • No return type for swap? Commented Jan 26, 2021 at 8:18

3 Answers 3

1

C is strictly pass by value. You pass the values of ptsr[0] and pstr[1] to swap. It swaps where it keeps those two values, but that has no effect on the calling function. Consider:

swap (int v1, int v2)
{
    int t;
    t = v1;
    v1 = v2;
    v2 = t;
}

This is the same as your swap function, just using int instead. It should be pretty clear that if you call swap(1,2);, the swap function just puts the 2 where it was storing the 1 and vice versa, but that has no effect on anything in the caller.

Same if you do this:

int i = 2;
int j = 3;
swap(i,j);

Since all you passed to swap is the values 2 and 3, it cannot affect the values of i and j by any means.

And same with your swap function if you do this:

char* j = "hello";
char* k = "world";
swap(j,k);

The function receives "hello" and "world" and swaps where it stores those two pointers. This has no effect on j or k in the caller.

C is strictly pass by value. Whatever parameters you pass to a function, the function only receives the values of.

Sign up to request clarification or add additional context in comments.

Comments

1

The function in the question only swaps the values of its argument. The arguments are copies of the array elements, so the swap function has no effect.

To swap the pointers in the array, you must pass their addresses and change the prototype of the swap function:

#include <stdio.h>

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

int main() {
    char *ptsr[2] = { "x", "y" };

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

    printf("pstr: { \"%s\", \"%s\" }\n", pstr[0], pstr[1]);
    return 0;
}

Comments

1

Functions accept their arguments by value.

That is the function swap (for which you forgot to specify the return type void)

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

deals with copies of values of the expressions ptsr[0] and ptsr[1]. Chamging the copies does not influence on the original pointers.

You may imagine the function definition and its call the following way

swap(ptsr[0], ptsr[1]);

//...

void swap( /*char *t1, char *t2*/) {
    char *t1 = ptsr[0], *t2 = ptsr[1];
    char *t;
    t = t1;
    t1 = t2;
    t2 = t;
}

As you can see the variables and ptsr[0] and ptsr[1] were not be changed.

To change an object (that in particularly can have a pointer type) in a function you need to pass it to the function by reference.

In C passing by reference means passing an object indirectly through a pointer to it.

So the function swap will look like

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

and the function must be called .like

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

Or as the same

swap(ptsr, ptsr + 1);

Dereferencing the pointers to pointers t1 and t2 the function gets a direct access to the original pointer ptsr[0] and ptsr[1] swapping their values.

Comments

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.