2
#include<stdio.h>
#include<string.h>

void* swap(void* x, void* y, int size)
{
    char* buffer[size];
    memcpy(buffer,x,size);
    memcpy(x,y,size);
    memcpy(y,buffer,size);

}

int main()
{
    char* wife = strdup("Wilma Deloitte");
    char* husband = strdup("Fred");
    printf("\n wife : %s husband : %s \n",wife,husband);
    swap(wife,husband,sizeof(char*));
    printf("\nAfter swap: \nwife : %s husband : %s \n",wife,husband);
    printf("\n sieof : %d \n",(int)sizeof(char*));
    return 0;
}

OUTPUT:

     wife : Wilma Deloitte husband : Fred 

After swap :

     wife : Fred husband : Wilma De 

     sieof : 8 

If I pass the &wife and &husband, then the address present inside the husband and wife gets swapped and hence I get the correct output with the fred and wilma deloitte strings swapped .. since the wife and the husband strings are pointing at the address of the fred and wilma deloitte ...

I have passed the address present inside the husband and the wife to be swapped by the memcpy .. I suppose 5 letters (since "fred\0" is 5 char) to be swapped between fred and wilma deloitte .. So the above ouput has to be Fred Deloitte and Wilma.. Why is the output not as expected..

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
Angus
  • 12,133
  • 29
  • 96
  • 151
  • 2
    Possible duplicate of [Different answers from strlen and sizeof for Pointer & Array based init of String](http://stackoverflow.com/questions/5925927/different-answers-from-strlen-and-sizeof-for-pointer-array-based-init-of-strin) – Steve Fallows Oct 22 '15 at 19:00

4 Answers4

4

You are swapping a number of bytes determined by sizeof(char*).

On your platform, sizeof(char*), and indeed, the size of ANY pointer is always 8 bytes.

sizeof is not a tool for measuring strings. If you meant to measure the length of the strings, you should use strlen, and not sizeof

If you wish to only swap their first names, try this:

swap(wife,husband,sizeof(husband)+1); // Swap 5 characters

void* swap(void* x, void* y, int size)
{
    char buffer[size];
    memcpy(buffer,x,size);  // Buffer is now "Wilma " (no NULL-terminator)
    memcpy(x,y,size);       // x is now "Fred Deloitte"
    memcpy(y,buffer,size);  // y is now "Wilma" (no NULL-terminator)
}

Note the comments about the NULL-terminators.
It is better, but not perfect.


If your intention was to swap the entire text, recognize that wife holds 15 characters, but husband only holds 5. How would you propose stuffing 15-characters of data into a 5-character array?

If you are operating on strings, I recommend using string-related functions: strlen, strcpy, strcat, and not mem-functions

abelenky
  • 63,815
  • 23
  • 109
  • 159
3

Change this:

char* buffer[size];

To this:

char buffer[size];

And change this:

swap(wife,husband,sizeof(char*));

To this:

swap(&wife,&husband,sizeof(char*));
dbush
  • 205,898
  • 23
  • 218
  • 273
1
swap(wife,husband,sizeof(char*));

when you send sizeof(char*) as size, it creates a buffer of 4 size, thats why only four chars are swapped.

It should be like below:

int size = strlen(wife)>strlen(husband) ? strlen(wife) : strlen(husband);
swap(wife,husband, size);
# maximum size
Shahriar
  • 13,460
  • 8
  • 78
  • 95
1

Most importantly, I believe your intent is to swap the contents of one dynamically allocated string with the contents of another dynamically allocated string. That is wrong. In the example program, you are trying to copy a 15-byte string ("Wilma Deloitte\0") into a 5-byte strdup()/malloc()ed string (the memory containing "Fred\0"). Once you fix the problems mentioned by others, you still have this very likely problem of corrupting the memory heap (depending on the malloc() implementation).

Alex Measday
  • 231
  • 1
  • 2