0

I made a code to swap two strings:

void swap (char *a, char *b)
{
    char *t = a;
    a = b;
    b = t;
}

int main()
{
    char * strings[2];
    strings [0] = "luck!";
    strings [1] = "good ";
    swap (strings[0], strings[1]);
    printf( "%s %s\n",strings[0], strings[1]);
    return 0;
}

And it fails. What i have trouble understanding is when i call swap() i pass two pointers. Both pointers point to the first character of their assigned arrays. I then created a temporary pointer inside the function and perform basic switch. What is flawed here? I really want to understand why this approach is wrong?

Tom
  • 9,275
  • 25
  • 89
  • 147

4 Answers4

7

You are switching the parameters of the function, which are local to the function scope. When your function is executed, the parameters (a, of type char*, and b, of type char*) are passed by value, put on the stack, and the function is executed. The parameters are modified and then popped of the stack without effect.

To make a difference, you need to pass references to the parameters:

void swap (char **a, char **b)
{
    char *t = *a;
    *a = *b;
    *b = t;
}

and then call with:

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

You now pass pointers to individual array elements in strings, which is in main's stack segment and thereby persists past the context of swap.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • But please note that it doesn't make much sense in a real world application to swap two pointers like this. – Lundin Jan 30 '13 at 15:23
0

You pass a copy to the pointers to the function and thus you exchange the copies. In C you will have to pass a pointer to the pointers to actually swap them.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • So the pointers themselves are copied? I thought that passing an array happens by reference rather than by value – Tom Jan 30 '13 at 13:50
  • Yes but you are not passing an array, you are passing one of array's elements. Keep in mind your array stores pointers so in fact a char pointer is an element instead of pointer the the whole array. – Ivaylo Strandjev Jan 30 '13 at 13:51
  • 1
    @Tom: pointers themselves are always passed by value. It's not entirely correct to claim that "arrays are passed by reference", however. Arrays decay to pointers in most contexts, so you are really passing a pointer by value instead of the whole array. – Blagovest Buyukliev Jan 30 '13 at 13:54
0

If you want to swap values through pointers, you need to use assigments of the form

*a = *b;

If you would do that in your code, it would swap the first characters of the strings. To swap the pointers, you need to have paraemters of type char**, and pass &strings[0].

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
0

Just to give you example, about what other answers suggesting ...

Update your swap function to

void swap (char **a, char **b)
{
    char *t = *a;
    *a = *b;
    *b = t;
}

And then call it in main as

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

However, you may want to update assignment of strings as string like "luck!" is constant and you cannot update its individual characters.

strings [0] = strdup("luck!");
strings [1] = strdup("good ");
Rohan
  • 52,392
  • 12
  • 90
  • 87