c语言使用指针交换数值


练习题:将两个int类型数值交换

#include <stdio.h>

void swap(int*,int*);

int main(void){

    int bin = 107, hex = 0x5f1043;
    
    swap(&bin,&hex);
    
    printf("%d %d\n",bin,hex);    

    return 0;
}

void swap(int *p1, int *p2)
{
    int tmp = *p1;
    *p1 = *p2;
    *p2 =tmp;
}

练习题:将两个字符串交换

#include <stdio.h>

void swap(char**,char**);

int main(void){

    char *sz1 ="World\0";
    char *sz2 = "Hello\0";

    swap(&sz1,&sz2);

    printf("%s %s\n",sz1,sz2);

    return 0;
}

void swap(char **sz1, char **sz2)
{
    char *tmp = *sz1;
    *sz1 = *sz2;
    *sz2 = tmp;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM