Hi Guys,
I am working on this code, and it is working fine. However, I would like to how it actually works.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include<String.h>
/*
*
*/
void swap(void *,void*,int);
int main(int argc, char** argv) {
char *husband = "Fred";
char *wife = "Wilma";
swap(&husband, &wife,sizeof(char *));
printf("The husband is: %s\n", husband);
printf("The wife is: %s\n", wife);
return (EXIT_SUCCESS);
}
void swap(void *vp1, void *vp2, int size){
char buf[size];
//copy the content of vp1 to buff
memcpy(buf,vp1,size);
memcpy(vp1,vp2,size);
memcpy(vp2,buf,size);
}
I understand that - The buf should have a pattern of whatever in vp1. But I can not confirmed it. I am assuming that - the buf should have a copy of the address of whatever in vp1. But how can I print the content of the buf out ?
I have tried:
printf("buf has: %p", buf) and I am getting: 0x22cce0
printf("vp1 has: %p",vp1) and I am getting: 0x22cd34
I thought: the buf and the vp1 should have the same bit pattern in them.
I have tried:
printf("buf has: %p", *buf); and I am getting: 0x20
Would some one help ?
Thanks,
z