-1
#include<stdio.h>
 
void swap (char *x, char *y)
{
    char *t = x;
    x = y;
    y = t;
}
 
int main()
{
    char *x = "geeksquiz";
    char *y = "geeksforgeeks";
    char *t;
    swap(x, y);
    printf("(%s, %s)", x, y);
    t = x;
    x = y;
    y = t;
    printf("n(%s, %s)", x, y);
    return 0;
}

I would expect that the original pointers would swap but it isn't the case here even though i pass the pointer to the function, The reason for it is that it makes local pointers ? How do i swap the original pointers using the function?

  • 1
    Inside `swap`, `x` and `y` are local variables. So, in reality, that function is a no-op in the grand scheme of things. – costaparas Dec 18 '20 at 13:48
  • Also check [this](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) post and [this](https://stackoverflow.com/questions/61013632/pointer-confusion-swap-method-in-c) post. – costaparas Dec 18 '20 at 13:51
  • Also, please avoid assign literal strings to `char*`. Consider assigning to `const char*` instead, or declaring it as an array, if you need to modify it. – DarkAtom Dec 18 '20 at 14:07

2 Answers2

3

The reason for it is that it makes local pointers ?

Yes. x, y and t are all local variables and x and y are copies of the original pointers.

How do i swap the original pointers using the function?

Swapping pointers probably doesn't make any sense, but if you some reason need to do it, then:

void swap (char** x, char** y)
{
  char* t = *x;
  *x = *y;
  *y = t;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

If you want to swap pointers you need to pass references to them (pointer to pointer)

void swap (char **x, char **y)
{
    char *t = *x;
    *x = *y;
    *y = t;
}

usage:

swap(&x, &y);

or you can define the macro:

#define  SWAP(x,y,type) do {type t; t = (x); (x) = (y); (y) = t; } while(0)

usage:

SWAP(x,y, char *);

or

#define  SWAP(x,y) do {const void * t; t = (const void *)(x); (x) = (y); (y) = t; } while(0)

usage:

SWAP(x,y);
0___________
  • 60,014
  • 4
  • 34
  • 74
  • `(void *)(x)` I would drop this cast. Suppose the pointers are qualified or if the caller pass an integer instead of a pointer - you'll hide away compiler errors. – Lundin Dec 18 '20 at 13:54
  • (After edit) And now you get the problem of assigning `const void*` to a non-qualified pointer :) All of this shows how brittle macros are - to make the macro type safe and cover all use-cases, we'd have to do a whole lot of other macro tricks. – Lundin Dec 18 '20 at 14:07
  • @Lundin First version then – 0___________ Dec 18 '20 at 14:23
  • Writing that macro type safe with 2 parameters is actually an interesting programming challenge. I can't think of any smooth way to do it. – Lundin Dec 18 '20 at 14:29
  • Actually, this might be well-defined: `uintptr_t t; memcpy(&t, &(x), sizeof(x)); (x) = (y); memcpy(&(y), &t), sizeof(y));` – Lundin Dec 18 '20 at 14:33
  • @Lundin `t` will be large enough to hold the pointer converted to the integer value. It does not have to be large enough to accommodate the pointer itself. – 0___________ Dec 18 '20 at 14:51