值交换和字符交换的对比

void swap_int(int a, int b) 

    int temp = a; 
    a = b; 
    b = temp; 

void swap_int(int *a, int *b) 

    int temp ;
 temp=*a;
 *a=*b;
 *b=temp;
}

void swap_str(char *a, char *b)  //两字符串没有交换
{  //交换了两个字符串的地址,按值传递
    char *temp = a; 
    a = b; 
    b = temp; 

 void swap_str(char **a, char **b) 

    char *temp ;
 temp=*a;
    *a = *b; 
    *b = temp; 
}
int main() 

    int a = 10; 
    int b = 5; 
    int a1=2;
    int b1=3;
    char *str_a = "hello world"; 
    char *str_b = "world hello"; 

    swap_int(a, b);  
    swap_str(str_a, str_b); 
    printf("%d, %d,%d, %d, %s, %s", a, b, a1,b1,str_a, str_b);      //10, 5, hello world, world hello
//都是按值传递,不改变原值

  swap_int(&a1,&b1);
 printf("%d, %d ,a1,b1);  //3,2


 char *str_a1= "hello ";  
   char *str_b1= "world ";    
 swap_str(&str_a1, &str_b1);  
 printf(" %s, %s", str_a1, str_b1);//world, hello
    return 0; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值