1

i have to char[] in C and i wanted to swap between them, by only swaping the pointer to the array and not one char at a time so i wrote this code:

#include <stdio.h>
void fastSwap (char **i, char **d)
{
    char *t = *d;
    *d = *i;
    *i = t;
}
int main ()
{
    char num1[] = "012345678910";
    char num2[] = "abcdefghujk";
    fastSwap ((char**)&num1,(char**)&num2);
    printf ("%s\n",num1);
    printf ("%s\n",num2);
    return 0;
}

I get this output (note the last 4 characters)

abcdefgh8910
01234567ujk

When I expect:

abcdefghujk
012345678910

NOTE: I am working on a 64 bit Linux System.

3
  • 6
    What's the question?
    – Scott
    Sep 28, 2010 at 19:21
  • 12
    Your two casts "(char**)" are a clue. When you lie to the compiler it will punish you.
    – Darron
    Sep 28, 2010 at 19:54
  • 4
    Don't forget to rename it to slowSwap() once you get it working.
    – ruslik
    Sep 28, 2010 at 20:30

9 Answers 9

12

You can't modify the addresses of num1 and num2, your code should work if your test was instead:

int main ()
{
    char num1[] = "012345678910";
    char num2[] = "abcdefghujk";
    char *test1 = num1;
    char *test2 = num2;
    fastSwap (&test1,&test2);
    printf ("%s\n",test1);
    printf ("%s\n",test2);
    return 0;
}
10
  • 1
    Look again at his output. I was fooled, too.
    – sbi
    Sep 28, 2010 at 19:25
  • 4
    @sbi the problem with the origin code is the swap of 'pointers' was actually swapping the first 8 bytes of the arrays due to the forced cast of &num1 ( which is a char * ) to a char **.
    – MerickOWA
    Sep 28, 2010 at 19:29
  • 1
    @bass - the compiler treats "&num1" to mean 'you want a pointer to the chars' but you force to to be a pointer to a pointer to the chars. Inside of fastswap 'char *t' is actually a bad pointer which contains 0x6162636465666768 (or 0x6867666564636261 on 64-bit little endian processors ).
    – MerickOWA
    Sep 28, 2010 at 19:38
  • 1
    @bass: (1) You need to turn your...thing into a real question. Seeing that the code does indeed swap, I wonder what you are asking. (2) When replying to a comment, you need to properly @address people. As it is, Merick would never see you asked him, unless he comes back here and looks. (3) If Merick is right (and I believe he is), you're invoking Undefined Behavior. So that your code actually swaps is by accident. It might just as well format your HD. Make the arrays longer and it will behave funny.
    – sbi
    Sep 28, 2010 at 20:09
  • 3
    This answer would be much more useful if it went into why the original code didn't work, rather than just presenting fixed code. This is how cargo cults get started. Sep 28, 2010 at 22:25
7

Arrays are not pointers. While they decay to pointers when you call your fastSwap(), these pointers are not the actual arrays. The fact that you need a cast should give you a hint that something is wrong.

This would work:

void fastSwap (const char **i, const char **d)
{
    const char *t = *d;
    *d = *i;
    *i = t;
}

const char* num1 = "012345678910";
const char* num2 = "abcdefghujk";
fastSwap (&num1,&num2);
printf ("%s\n",num1);
printf ("%s\n",num2);
4
  • have you actually compiled and ran it?
    – florin
    Sep 28, 2010 at 19:25
  • I would have expected an error on the const-ness of your pointers. Sep 28, 2010 at 22:21
  • There should at least be a warning, for passing const char ** values to a function expecting char **.
    – caf
    Sep 29, 2010 at 6:32
  • @florin, @Mark, @caf: You're right! I had to change the fastSwap() function as well, but forgot about that! Added it now.
    – sbi
    Sep 29, 2010 at 7:58
5

This will work:

int main ()
{
    char *num1 = "012345678910";
    char *num2 = "abcdefghujk";
    fastSwap (&num1,&num2);
    printf ("%s\n",num1);
    printf ("%s\n",num2);
    return 0;
}
4

num1 is an array, and &num1 is the address of the array itself - it is not an address of a pointer.

The address of an array itself is the same location in memory as the address of the first element of the array, but it has a different type. When you cast that address to char **, you are claiming that it points at a char * value - but it does not. It points at a block of 13 chars. Your swap function then accesses that array of 13 chars as if it were a char * - since the latter is the same size as 8 chars on your platform, you end up swapping the first 8 chars of each array.

3

Your fastSwap only seems to work. You're invoking undefined behavior by casting '&num1' and '&num2' (which are pointers to the characters of num1 and num2) to pointers to pointers of characters (char**).

char *t = *d

t will point to whatever d's contents are pointing to, however d is pointing to the actually characters of num2 ("abcdefghujk" or 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x75 0x6B 0x00). This means that '*d' is actually copying the contents of 'num2' and not the pointer to num2 as you probably expected.

't' then is a bad pointer however since it is never dereferenced you avoid a crash/segment fault.

Because you're on a 64 bit machine/OS pointers are 8 bytes the value of 't' is now the first 8 bytes of 'num2' and this is what gets put into num1 after

*i = t

If you intend to swap pointers you must first create pointer variables as Mark did

char *test1 = num1;
char *test2 = num2;
fastSwap (&test1,&test2);

Or change num1 and num2 into pointers (char *) rather than arrays (char[]) as sb1/Karl did

char *num1 = "012345678910";
char *num2 = "abcdefghujk";
fastSwap (&num1,&num2);
1

I've had the same situation and solved it with the following trick:

p.s. windows platform, vs-2012

void FastSwap (void **var1, void **var2) {
    void *pTmp = *var1;
    *var1 = *var2;
    *var2 = pTmp;
}

int main () {
    char *s1 = "1234567890123456";
    char *s2 = "abcdefghij";
    printf ("s1 before swap: \"%s\"\n", s1);
    printf ("s2 before swap: \"%s\"\n", s2);
    // if you change arguments in the FastSwap definition as (char **) 
    // then you can erase the (void **) part on the line below.
    FastSwap ((void **) &s1, (void **) &s2);
    printf ("s1 after swap : \"%s\"\n", s1);
    printf ("s2 after swap : \"%s\"\n", s2);
    return (0);
}
0

Change fastSwap to:

void fastSwap (char *i, char *d)
{
   while ((*i) && (*d))
   {
      char t = *d;
      *d = *i;
      *i = t;

      i ++;
      d ++;
   }
}

then call fastSwap (num1, num2);

1
  • 13
    ...and rename it to slowSwap().
    – sbi
    Sep 28, 2010 at 19:22
0
int swap(char *a, char *b){
    char *temp = (char *)malloc((strlen(a) + 1)*sizeof(char));
    strcpy(temp, a);
    strcpy(a, b);
    strcpy(b, temp);
}
int main(){
    char a[10] = "stack";
    char b[10] = "overflow";
    swap(a, b);
    printf("%s %s", a, b);
}
2
  • 3
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – HMD
    Dec 26, 2018 at 7:52
  • 1
    I guess your swap() function will fail if in main() you will have something like: char a[] = "stack"; char b[] = "overflow;" Dec 26, 2018 at 8:18
0

While given answers already showed how to do it right (and hinted to undefined behaviour), you still might be interested in the details...

At very first, you have to character arrays. Applying the addressof operator to won't give you a pointer to pointer, but a pointer to array:

int(*ptr1)[10] = &num1;

Syntax might look strange, but that's C. The important thing here now is: You have a pointer with one level of indirection. But you cast it to a pointer with two levels of indirection (char**).

What now happens in fastSwap is:

char* t = *d;

This will copy as many bytes of *d into t as pointers on your system have size. Solely: In reality, you do not have a pointer to pointer, but a pointer to array which only has been casted. So the first sizeof(void*) bytes of the array will be copied into t. Similarly for the other assignments, explaining the results you get.

If now your arrays were shorter than the size of pointers, then memory after the arrays would have been read:

int a[] = "123";
int b[] = "456";
int c[] = "789";

fastSwap ((char**)&a, char**(&b));
printf("%s %s %s", a, b, c);

would have printed on your system (as pointer size is 8 bytes):

456 789 456

Explanation:

char *t = *d;
// copied the four bytes of b into t - AND the next four bytes (those of c!!!)

*d = *i;
// copied the eight bytes of a AND b int b AND c
// b contains "123", c "456"

*i = t;
// copied the eight bytes that were copied from b and c into a and b
// the "123" in b now got overwritten, that's why you never see them... 

Be aware that you don't have guarantee for such result, it is just the most likely one. You invoked undefined behaviour, anything might happen instead, you even might switch off the sun by accident (if your compiler happened to produce the appropriate code...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.