- Practice integer swap and string swap
void swap_int(int *a, int *b)
{
int tmp = 0;
tmp = *a;
*a = *b;
*b = tmp;
return;
}
int main(int argc, char *argv[])
{
// 2 integer number swap
int num_a = 25;
int num_b = 82;
printf("origianl: num_a=%d %x, num_b=%d %x\r\n", num_a, &num_a, num_b, &num_b);
swap_int(&num_a, &num_b);
printf("swap: num_a=%d %x, num_b=%d %x\r\n", num_a, &num_a, num_b, &num_b);
return 0;
}
結果: [jenson_hu@sw6-builder3 c_string]$ ./c_reverse
origianl: num_a=25 babf075c, num_b=82 babf0758
swap: num_a=82 babf075c, num_b=25 babf0758
void swap_str(char **str_a, char **str_b)
{
printf("=== befor exhange ==== \r\n");
printf("function: str_a=%x *str_a=%x *str_a=%s %x\r\n", str_a, *str_a, *str_a);
printf("funciton: str_b=%x *str_b=%x *str_b=%s \r\n", str_b, *str_b, *str_b);
char *tmp;
tmp = *str_a;
*str_a = *str_b;
*str_b = tmp;
printf("=== after exhange ==== \r\n");
printf("function: str_a=%x *str_a=%x *str_a=%s \r\n", str_a, *str_a, *str_a);
printf("funciton: str_b=%x *str_b=%x *str_b=%s \r\n", str_b, *str_b, *str_b);
return;
}
int main(int argc, char *argv[])
{
// The 2 string swap
char a[] = "apple";
char b[] = "samsung";
char *str_a = &a;
char *str_b = &b;
swap_str(&str_a, &str_b);
return 0;
}
=== befor exhange ====
function: str_a=babf0738 *str_a=babf0750 *str_a=apple 3d3d2065
funciton: str_b=babf0730 *str_b=babf0740 *str_b=samsung
=== after exhange ====
function: str_a=babf0738 *str_a=babf0740 *str_a=samsung
funciton: str_b=babf0730 *str_b=babf0750 *str_b=apple